// 连接两个数组
const a1 = [1, 2, 3];
const a2 = [4, 5, 6];
const a3 = a1.concat(a2);
console.log(a3);
console.log(
"----------------------------------------------------------------------"
);
const b1 = ["red", "orange", "yellow", "green", "blue", "dian", "zi"];
// 将数组的前两个元素赋值到第三,四个位置,那么原来三四两个位置的元素就是会丢失
// b1 => ["red", "orange", "yellow", "green", "blue", "dian", "zi"];
// "yellow", "green" : 就是被覆盖掉
// "red", "orange" : 新复制过来的内容
// b1 => ["red", "orange", "red", "orange", "blue", "dian", "zi"];
b1.copyWithin(2, 0, 2);
console.log(b1);
console.log(
"----------------------------------------------------------------------"
);
const iterator = b1.entries();
console.dir(iterator.next().value);
console.dir(iterator.next());
console.dir(iterator.next());
console.dir(iterator.next());
console.dir(iterator.next());
console.dir(iterator.next());
console.dir(iterator.next());
console.dir(iterator.next());
console.log(
"----------------------------------------------------------------------"
);
const ages = [19, 43, 12, 32, 13, 15, 39];
// every 用来检测数组中每个元素是否符合条件,如果有一个不符合条件,就是返回false
// 如果回调函数中有一个返回false,那么整体就返回false
const res = ages.every((item, index) => {
return item > 12;
});
console.log(res);
console.log(
"----------------------------------------------------------------------"
);
const fruits = ["alice", 123, 2, 23];
fruits.fill("Branana");
console.log(fruits);
console.log(
"----------------------------------------------------------------------"
);
// 创建一个二维数组,然后填充
const c1 = new Array(3).fill("alice");
console.log(c1);
const c2 = Array(3).fill("alice");
console.log(c2);
const c3 = Array.from(Array(3), () => new Array(4).fill(0));
console.log(c3);
c3[1][1] = 1;
console.log(c3);
// fill参数是一个引用引用类型,所以会相互影响,也就是创建二维数组的时候,fill只能使用一次
const c4 = new Array(3).fill(new Array(4).fill(0));
console.dir(c4);
c4[1][1] = 1;
console.dir(c4);
console.log(
"----------------------------------------------------------------------"
);
/**
* Creates an array from an iterable object.
* @param iterable An iterable object to convert to an array.
* @param mapfn A mapping function to call // on every element of the array.
* @param thisArg Value of 'this' used to invoke the mapfn.
*/
const d1 = Array.from(["alice", "bruce", "celina"], (item) => {
console.log(item);
return item + "666";
});
console.log(d1);
const d2 = Array.from("alice", (item) => {
console.log(item);
return item + "_666";
});
console.log(d2);
// 第一个参数创建一个数组,然后每次都会调用后面的函数
const d3 = Array.from(Array(4), (item, index) => {
if (index === 1) {
return new Array(4).fill(666);
} else {
return new Array(4).fill(0);
}
});
console.log(d3);
console.log(
"----------------------------------------------------------------------"
);
const e1 = ["red", "orange", "yellow", "green", "blue", "dian", "zi"];
for (let key of e1.keys()) {
console.log(e1[key]);
}
const e2 = ["red", "orange", "yellow", "green", "blue", "dian", "zi"];
e2.forEach((v, i) => {
console.log(i + "__" + v);
});
const e3 = ["red", "orange", "yellow", "green", "blue", "dian", "zi"];
const e4 = e3.map((v, k) => {
if (k === 1) {
return "666";
}
return v;
});
console.log(e4);