//from()用于将 类数组结构转换为数组实例 //第一个参数是一个类数组对象,即任何可迭代的结构,或者有一个 length 属性 和可索引元素的结构 //不是数组的转化为数组 let arr = Array.from("Gaofeng"); console.log(arr); //["G", "a", "o", "f", "e", "n", "g"]; const m = new Map().set(1, 2); const b = new Map().set(1, 2).set(3, 4); console.log(m); //Map { 1 => 2 } console.log(b); //Map { 1 => 2, 3 => 4 } console.log(Array.from(b)); //[ [ 1, 2 ], [ 3, 4 ] ] const c = new Set().add(1).add(2).add(3).add(4); console.log(Array.from(c)); //[ 1, 2, 3, 4 ] Array.from()对现有数组执行浅复制 const a1 = [1, 2, 3, 4, { name: "gaofeng" }]; const a2 = Array.from(a1); a1.push(5); a1[4].name = "zhangsan"; console.log(a1); // [1, 2, 3, 4, { name: zhangsan }, 5 ] console.log(a2); //[ 1, 2, 3, 4, { name: zhangsan } ] console.log(a1 == a2); const c1 = [1, 2, 3, 4]; const a4 = Array.from(c1, (x) => x ** 2); console.log(a4); //[ 1, 4, 9, 16 ] // console.log(Array.of(1, 2, 3, 4)); //[ 1, 2, 3, 4 ] //数组填充 const zeroes = [0, 0, 0, 0, 0]; zeroes.fill(7); console.log(zeroes); //[ 7, 7, 7, 7, 7 ] zeroes.fill(0); console.log(zeroes); //[ 0, 0, 0, 0, 0 ] // zeroes.fill(6, 3); console.log(zeroes); //[ 0, 0, 0, 6, 6 ] /* let colors = ["red", "blue", "green"]; console.log(colors.toString()); console.log(colors.valueOf()); */ let colors = new Array(); let count = colors.unshift("red", "green"); // 从数组开头 console.log(colors); /* let values = [1, 2, 3, 4, 5]; let sum = values.reduce((prev, cur, index, array) => prev + cur, 10); console.log(sum); */ /* let values = [1, 2, 3, 4, 5]; let sum = values.reduce((prev, cur, index, array) => prev + cur, 10); console.log(sum, values); */ let values = [{ num: 0 }, { num: 1 }, { num: 2 }, { num: 3 }, { num: 4 }]; let sum = values.reduce( (prev, cur, index, array) => { console.log(prev, cur); return { num: prev.num + cur.num }; }, { num: 20 } ); console.log(sum.num, values);
持续总结中…