字面量
let arr = [1, 2, "a", "b", true, false, { x: 1, y: 2 }, [1, 2], function () {}];
console.log(arr);
…rest
arr = [4, 5, 6];
let arr1 = [...arr];
console.log(arr1);
Array.of
let a = [1, 2, 3, 4, 5, 6, true, function () {}];
arr = Array.of(...a);
console.log(arr);
Array.from
const likeArr = {
0: "家",
1: "公司",
2: "学校",
length: 3,
};
console.log(likeArr);
console.log(Array.from(likeArr));
尾部增删,push从左到右进入,pop从右到左删除
let arr = [];
console.log(arr.push(1));
console.log(arr.push(2, 3));
console.log(arr);
console.log(arr.pop());
console.log(arr.pop());
console.log(arr.pop());
头部增删,unshift从右到左增加,shift从左到右删除
console.log(arr.unshift(1));
console.log(arr.unshift(3, 2));
console.log(arr);
console.log(arr.shift());
console.log(arr.shift());
console.log(arr.shift());
console.log(arr);
delete:删除指定的
arr = [1, 2, 3, 4, 5, 6, 7];
delete arr[4];
console.log(arr);
arr.length = 4;
console.log(arr);
forEach((item,index,arr)=>{…})每个元素逐个调用,没有返回值
let arr = [4, 5, 6, 7];
arr.forEach((item, index, arr) => console.log(item, index, arr));
map;参数与功能与forEach一样,只是有返回值
res = arr.map(item => item * 2);
console.log(res);
every,some 断言函数,返回true,falseevery:数组成员全部满足条件,返回true,否则false
console.log(arr.every(item => item >= 0));
console.log(arr.every(item => item >= 5));
some:数组成员有一个满足条件,返回true,否则false
console.log(arr.some(item => item >= 6));
console.log(arr.some(item => item >= 8));
filter:返回数组满足条件的元素,组成新数组,find返回新数组的第一个元素,findIndex返回新数组的第一个索引
console.log(arr.filter(item => item >= 5));
console.log(arr.find(item => item >= 6));
console.log(arr.findIndex(item => item >= 6));
reduce 归并
res = arr.reduce(function (acc, cur, index, arr) {
return acc + cur;
}, 5);
console.log(res);
sort 排序
let arr = [20, 30, 14, 53];
console.log(arr.sort());
console.log(arr.sort((a, b) => a - b));//升序
console.log(arr.sort((a, b) => b - a));//降序
join:array->String
arr = ["a", "b", "c"];
console.log(arr.join());
console.log(arr.join("-"));
splice:删除,新增,替换
console.log(arr);
console.log(arr.slice(5, 2));
console.log(arr);
console.log(arr.splice(1, 2, "a", "b"));
console.log(arr);
console.log(arr.slice(2, 0, "red", "green"));
console.log(arr);
let data = ["red", "green", "blue"];
console.log(arr.splice(2, 0, ...data));
console.log(arr);
相关推荐
© 2020 asciim码
人生就是一场修行