console.log("**************数组方法比较***************");
{var arr = [1, 2, 3, 4, 5, 6];console.log(arr.some(item => {return item > 3;}));// 输出结果:true//some判断数组中的每一项是否满足条件,遇到第一个满足条件的项,就立刻跳出循环,返回 true,都不满足条件,返回false
}
{var arr = [1, 2, 3, 4, 5, 6];console.log(arr.every(item => {return item > 3;}));// 输出结果:false//every判断数组中的所有项是否都满足条件,都满足返回true,如果有一项不满足,就立刻跳出循环,返回false
}
{var arr = [1, 2, 3, 4, 5, 6];console.log(arr.map(item => {return item > 3;}));// 输出结果:[false, false, false, true, true, true]//map依次判断每一项,直到最后一项,返回一个新数组,这个新数组的每一项为依次判断的结果,满足条件为true,不满足条件为false
}
{var arr = [1, 2, 3, 4, 5, 6];console.log(arr.filter(item => {return item > 3;}));// 输出结果: [4, 5, 6]//filter判断每项是否满足条件,返回一个新数组,把满足条件的放到这个新数组里
}
{var arr = [1, 2, 3, 4, 5, 6];console.log(arr.includes(3));//includes 的作用是判断数组中是否存在某个值,返回true/false,some是判断数组中的每项是否满足提交,如果遇到都一个满足条件的就返回true,都不满足返回false,every的作用是判断数组中的每一项是否都满足条件,都满足返回true,有一项不满足,就跳出循环,并返回false,
}
{let arr = [1,[2, 3, ["b", "c", [33, 666, 99, [99, "J"]]]]]console.log(arr.flat());//[1,2,3,["b","c",[33,666,99,[99,"J"]]]]console.log(arr.flat(4));//[1, 2, 3, 'b', 'c', 33, 666, 99, 99, 'J']
}
{let arr = [{name: "01",list: ["安庆", "安阳", "鞍山"]}, {name: "02",list: ["湖州", "阜阳", "抚顺"]}]console.log(arr.flatMap(item => item.list));
}
{let list = [1, 2, 2, "kerwin", "kerwin", [1, 23, [3.4], [1, 2], 3, { name: "kerwin" }, { age: 100 }, { name: "kerwin" }], undefined, undefined, NaN, NaN]function uni(arr) {let res = new Set()return arr.filter(item => {let id = JSON.stringify(item)//判断has trun false//没有return trueif (res.has(id)) {return false} else {res.add(id)return true}})}console.log(uni(list))}
{let list = [1, 2, 3, 4, 5, 2, 499, 155, 66, 72, 666, 5, 2, 5, 6, 5, 55, 15, 56, 24, 5, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]let set = new Set(list)let newlist = [...set]console.log(newlist);console.log(Array.from(set));let sortlist = newlist.sort((a, b) => a - b)console.log(sortlist);
}
{let arr = [{ name: "01", age: 22 },{ name: "02", age: 2 },{ name: "03", age: 52 },{ name: "04", age: 13 },{ name: "05", age: 15 },]let newarr = arr.sort((a, b) => a.age - b.age)console.log(newarr);
}
// {
// // let p1 = () => {
// // return new Promise((resolve, reject) => {
// // setTimeout(() => {
// // resolve("p1---执行了")
// // }, 1000);
// // }).then((res) => {
// // console.log(res);
// // })
// // }
// // let p2 = () => {
// // return new Promise((resolve, reject) => {
// // setTimeout(() => {
// // resolve("p2---执行了")
// // }, 1000);
// // }).then((res) => {
// // console.log(res);
// // })
// // }
// // let p3 = () => {
// // return new Promise((resolve, reject) => {
// // setTimeout(() => {
// // //resolve("p3---执行了")
// // reject()
// // }, 1000);
// // }).then((res) => {
// // console.log(res);
// // })// // }
// let p1 = new Promise((resolve, reject) => {
// setTimeout(() => {
// resolve("p1---执行了")
// }, 1000);
// }).then((res) => {
// console.log(res);
// })
// let p2 = new Promise((resolve, reject) => {
// setTimeout(() => {
// resolve("p2---执行了")
// }, 1000);
// }).then((res) => {
// console.log(res);
// })
// let p3 = new Promise((resolve, reject) => {
// setTimeout(() => {
// //resolve("p3---执行了")
// reject()
// }, 1000);
// }).then((res) => {
// console.log(res);
// })
// Promise.all([p1, p2, p3]).then((res) => {
// console.log("%cPromise.all执行成功了", "color: red;font-size:16px", res)
// })
// .catch((error) => {
// console.log("%cPromise.all执行失败了", error)
// })
// // Promise.race([p1, p2, p3]).then((res) => {
// // console.log("%cPromise.race执行成功了", "color: red;font-size:16px", res)
// // })
// // .catch((error) => {
// // console.log("%cPromise.race执行失败了", error)
// // })
// // Promise.any([p1, p2, p3]).then((res) => {
// // console.log("%cPromise.any执行成功了", "color: red;font-size:16px", res)
// // })
// // .catch((error) => {
// // console.log("%cPromise.any执行失败了", error)
// // })
// // Promise.allSettled([p1, p2, p3]).then((res) => {
// // console.log("%cPromise.allSettled执行成功了", "color: red;font-size:16px", res)
// // })
// // .catch((error) => {
// // console.log("%cPromise.allSettled执行失败了", error)
// // })
// }
{let p1 = new Promise((resolve, reject) => {setTimeout(() => {resolve("p1--all---执行了")}, 1000);}).then((res) => {console.log(res);})let p2 = new Promise((resolve, reject) => {setTimeout(() => {resolve("p2--all---执行了")}, 1000);}).then((res) => {console.log(res);})let p3 = new Promise((resolve, reject) => {setTimeout(() => {//resolve("p3--all---执行了")reject()}, 1000);}).then((res) => {console.log(res);})Promise.all([p1, p2, p3]).then((res) => {console.log("%cPromise.all执行成功了", "color: green;font-size:22px", res)}).catch((error) => {console.log("%cPromise.all执行失败了", "color: red;font-size:22px", error)})}
{let p1 = new Promise((resolve, reject) => {setTimeout(() => {resolve("p1--race---执行了")}, 1000);}).then((res) => {console.log(res);})let p2 = new Promise((resolve, reject) => {setTimeout(() => {resolve("p2--race---执行了")}, 1000);}).then((res) => {console.log(res);})let p3 = new Promise((resolve, reject) => {setTimeout(() => {//resolve("p3--race---执行了")reject()}, 1000);}).then((res) => {console.log(res);})Promise.race([p1, p2, p3]).then((res) => {console.log("%cPromise.race执行成功了", "color: green;font-size:22px", res)}).catch((error) => {console.log("%cPromise.race执行失败了", "color: red;font-size:16px", error)})}
{let p1 = new Promise((resolve, reject) => {setTimeout(() => {resolve("p1--any---执行了")}, 1000);}).then((res) => {console.log(res);})let p2 = new Promise((resolve, reject) => {setTimeout(() => {resolve("p2--any---执行了")}, 1000);}).then((res) => {console.log(res);})let p3 = new Promise((resolve, reject) => {setTimeout(() => {//resolve("p3--any---执行了")reject()}, 1000);}).then((res) => {console.log(res);})Promise.any([p1, p2, p3]).then((res) => {console.log("%cPromise.any执行成功了", "color: green;font-size:22px", res)}).catch((error) => {console.log("%cPromise.any执行失败了", "color: red;font-size:16px", error)})}
{let p1 = new Promise((resolve, reject) => {setTimeout(() => {resolve("p1--allSettled---执行了")}, 1000);}).then((res) => {console.log(res);})let p2 = new Promise((resolve, reject) => {setTimeout(() => {resolve("p2--allSettled---执行了")}, 1000);}).then((res) => {console.log(res);})let p3 = new Promise((resolve, reject) => {setTimeout(() => {//resolve("p3--allSettled---执行了")reject()}, 1000);}).then((res) => {console.log(res);})Promise.allSettled([p1, p2, p3]).then((res) => {console.log("%cPromise.allSettled执行成功了", "color: green;font-size:22px", res)}).catch((error) => {console.log("%cPromise.allSettled执行失败了", "color: red;font-size:16px", error)})
}{function timer(t) {return new Promise(resolve => {setTimeout(() => {resolve(t)}, t)})}async function* fn() {yield timer(1000)//任务1yield timer(2000)//任务2yield timer(3000)//任务3}// 使用一下 for await ...ofasync function fn1() {for await (const val of fn()) {console.log("start", Date.now())console.log(val);console.log("end", Date.now())}}fn1();
}