1.数组去重 new Set()
var set = new Set([1,2,3,4,4,8,null,null,Array,Object,Object]);
console.log([...set]);
2.类似数组或可迭代对象创建一个新的,浅拷贝的数组 Array.from()
注意:创造出新的
console.log(Array.from('foo'));
console.log(Array.from([1, 2, 3], x => x + x));
Array [“f”, “o”, “o”]
Array [2, 4, 6]
3.判断是否为数组 Array.isArray(obj)
若obj为数组 则输出ture 否则输出false
例如:
Array.isArray([1, 2, 3]);
Array.isArray({foo: 123});
true
false
4.数组拼接合并 Array.concat()
注意:此方法不会更改现有数组,而是返回一个新数组
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);
Array [“a”, “b”, “c”, “d”, “e”, “f”]
5.检测数组所有元素是否都满足条件 arr.every()
注意 :若检测的arr数组为空时,在任何情况下它返回的都是true
const isBelowThreshold = (currentValue) => currentValue < 40; //需要满足的条件
const array1 = [1, 30, 39, 29, 10, 13];
console.log(array1.every(isBelowThreshold));
true
6.检测数组是否至少有1个元素满足条件 arr.some()
注意 :若检测的arr数组为空时,在任何情况下它返回的都是false
const isBelowThreshold = (currentValue) => currentValue < 10; //需要满足的条件
const array1 = [1, 30, 39, 29, 10, 13];
console.log(array1.some(isBelowThreshold));
true
7.数组排序 arr.sort(function)
function<object<null<Array<(string与number进行比较按转换为数字后的大小进行排序)<undefined
var arr=[1,41,10,5,6,"7",true,null,undefined,[],{},function(){}]
var b=arr.sort((a,b)=>{
return a-b
})
console.log(b)
Array [function(){}, Object { }, null, Array [], 1, true, 5, 6, “7”, 10, 41, undefined]