Open anjia opened 6 years ago
e.g.
const members = ['anjia', 'luoqiang', 'lijianchun'];
// 移除'anjia'
const withoutAnjia = members.filter(item => item!=='anjia');
console.log(withoutAnjia === members); // false
console.log(withoutAnjia, members);
// 把'anjia'替换成'zora'
const replaceAnjia = members.map(item => item==='anjia'?'zora':item);
console.log(replaceAnjia === members); // false
console.log(replaceAnjia, members);
// 把所有人名大写
const strongMembers = members.map(item => item.toUpperCase());
console.log(strongMembers === members); // false
console.log(strongMembers, members);
// 和另外一组进行合并
const otherMembers = ['peipei', 'yingying'];
const newMembers = [...members, ...otherMembers];
console.log(newMembers === members, newMembers === otherMembers); // false, false
console.log(newMembers, members);
for
最古老的循环,效率高。处理大量循环的时for-of
只遍历对象上的,ES6forEach
数组内部的方法。稀疏数组for-in
会遍历继承链的对象属性。遍历对象的属性,仅可枚举类型map
产生新数组。数组的每项经过计算,产生的新值filter
产生新数组。返回 true 的值reduce
返回值。前项加后项,累计计算最终值every
布尔值。遇到返回 false 的,就返回 false。 检测每一项是否符合条件some
布尔值。遇到返回 true 的,就返回 true。 检查是否有符合条件的项