类数组转化为数组
Array.from()
every()
判断数组中的元素是否都符合条件,数组中有个一个不符合条件就返回false
every(function,this指向)
返回值:true/false
var personArr = [
{ name: '12', src: '../12/123', des: '1221', sex: 'm', age: 10 },
{ name: '13', src: '../13/1234', des: '1222', sex: 'f', age: 15 },
{ name: '14', src: '../14/1234', des: '1223', sex: 'm', age: 23 },
{ name: '15', src: '../15/1245', des: '12214', sex: 'f', age: 14 },
];
var newPersonArr = personArr.every(function (ele, index, self) {
if (ele.age > 15) {
return false;
}
}, personArr)
console.log(newPersonArr)
every()原理
var personArr = [
{ name: '12', src: '../12/123', des: '1221', sex: 'm', age: 10 },
{ name: '13', src: '../13/1234', des: '1222', sex: 'f', age: 15 },
{ name: '14', src: '../14/1234', des: '1223', sex: 'm', age: 23 },
{ name: '15', src: '../15/1245', des: '12214', sex: 'f', age: 14 },
];
Array.prototype.myEvery = function(fun){
var _this = arguments[1] || window;
var len = this.length;
var bool = true;
for(var i = 0 ; i < len ; i++){
if(fun.apply(_this,[this[i],i,this]) == false){
bool = false;
break;
}
}
return bool
}
var newPersonArr = personArr.myEvery(function(ele,index,self){
if(ele.age > 15){
return false;
}
})
some()
与every()不同every 更像是逻辑运算符&& 而some则是逻辑运算符中的 || 当全为false时才会返回false,当有个一个满足条件时那就为true
some(function,this指向)
返回值:true/false
var personArr = [
{ name: '12', src: '../12/123', des: '1221', sex: 'm', age: 10 },
{ name: '13', src: '../13/1234', des: '1222', sex: 'f', age: 15 },
{ name: '14', src: '../14/1234', des: '1223', sex: 'm', age: 23 },
{ name: '15', src: '../15/1245', des: '12214', sex: 'f', age: 14 },
];
var newPersonArr = personArr.some(function (ele, index, self) {
if (ele.age < 11) {
return true;
}else{
return false;
}
}, personArr)
console.log(newPersonArr)
some() 原理
var personArr = [
{ name: '12', src: '../12/123', des: '1221', sex: 'm', age: 10 },
{ name: '13', src: '../13/1234', des: '1222', sex: 'f', age: 15 },
{ name: '14', src: '../14/1234', des: '1223', sex: 'm', age: 23 },
{ name: '15', src: '../15/1245', des: '12214', sex: 'f', age: 14 },
];
Array.prototype.mySome = function (fun) {
var _this = arguments[1] || window;
var len = this.length;
var bool = true;
for (var i = 0; i < len; i++) {
if (fun.apply(_this, [this[i], i, this]) == false) {
bool = false;
}else{
bool = true;
}
}
return bool
}
var newPersonArr = personArr.mySome(function (ele, index, self) {
if (ele.age < 15) {
return true;
}else{
return false;
}
})
indexOf()
方法返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回-1
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
console.log(beasts.indexOf('bison'));
// expected output: 1
split() 与 join()
split()按照指定的格式将string拆分为数组
var lrc = `[00:01.06]难念的经
[00:03.95]演唱:周华健
[00:06.78]
[00:30.96]笑你我枉花光心计
`;
var parts = lrc.split("\n");
// 结果为 parts["[00:01.06]难念的经","[00:03.95]演唱:周华健",“[00:06.78]”,“[00:30.96]笑你我枉花光心计”]
join()将数组按照自定义的格式拆分为string
var arr = [1,2,3,4,5,7];
var a = arr.join('-'); // a = "1-2-3-4-5-7"
concat()
拼接两个数组
var arr = [1,2,3,4,5,7]
var arrs = [1,2,3,4,5,6,7]
arr.concat(arrs);
push()与unshift() , pop() 与 shift()
push()方法将一个或多个元素添加到数组的末尾,并返回该数组的新长度
var arr = [];
arr.push("123");
unshift() 方法将一个或多个元素添加到数组的最前面,并返回该数组的新长度
var arr = [1,2,3,4,5,6,7]
arr.unshift(-1,0);
pop()将从数组最后一位剪贴出来且不接受参数
var arr = [1,2,3,4,5,6,7]
var arrs = [1,2,3,4,5,6,7]
var a = arr.pop(); //将数组最后一位剪贴出来
console.log(a);
arrs.pop(); // 将数组最后一位剪贴出来且不保存
console.log(arrs);
shift()将从数组第一位剪贴出来且不接受参数
var arr = [1,2,3,4,5,6,7]
var arrs = [1,2,3,4,5,6,7];
var a = arrs.shift(2);
var b = arr.shift();
console.log(a);
console.log(b);
reverse()
将数组内的顺序颠倒
var arr = [1,2,3,4,5,6,7]
arr.reverse(); //结果为 arr = [7, 6, 5, 4, 3, 2, 1];
splice()
splice(截取的位置,截取多少位,在其切口处添加数据/无限制的添加)
var arr = [1,2,3,4,5,7]
arr.splice(5,0,6); // arr = [1, 2, 3, 4, 5, 6, 7];
sort()
给数组排序,默认是以字符编码的顺序进行排序,也可以自定义
规则为:必须有两个参数
若 a 小于 b,在排序后的数组中 a 应该出现在 b 之前,则返回一个小于 0 的值。//通俗点讲就是,当返回值为负数时,前面的数在前面
若 a 等于 b,则返回 0。// 若返回值为0 ,不变
若 a 大于 b,则返回一个大于 0 的值 //返回值大于零 , 后面的数在前面
//自定义排序
var arr = [2,1,3];
arr.sort(function(a , b){
return a-b;
});
//结果为 arr = [1, 2, 3];