优秀的编程知识分享平台

网站首页 > 技术文章 正文

ES6常见的数组方法(es6数组和对象的方法)

nanyue 2024-10-11 13:39:41 技术文章 4 ℃

1. Array.from() 将类数组对象或可遍历对象转换成真正的数组

//1. Array.from() 将类数组对象或可遍历对象转换成真正的数组
const str = 'hello';
const arr = Array.from(str);
console.log(arr); // ['h', 'e', 'l', 'l', 'o']


2. Array.of() 创建一个包含任意数量参数的数组

//2. Array.of() 创建一个包含任意数量参数的数组
const arr1 = Array.of(1, 2, 3);
console.log(arr1); // [1, 2, 3]


3. Array.prototype.find() 返回数组中第一个满足条件的元素

//3. Array.prototype.find() 返回数组中第一个满足条件的元素
const arr2 = [1, 2, 3, 4, 5];
const result = arr2.find(item => item > 3);
console.log(result); // 4


4. Array.prototype.findIndex() 返回数组中第一个满足条件的元素的索引

//4. Array.prototype.findIndex() 返回数组中第一个满足条件的元素的索引
const arr3 = [1, 2, 3, 4, 5];
const index = arr3.findIndex(item => item > 3);
console.log(index); // 3


5. Array.prototype.fill() 用指定的值填充数组

//5. Array.prototype.fill() 用指定的值填充数组
const arr4 = [1, 2, 3, 4, 5];
arr4.fill(0, 2, 4);
console.log(arr4); // [1, 2, 0, 0, 5]


6. Array.prototype.includes() 判断数组是否包含指定的元素

//6. Array.prototype.includes() 判断数组是否包含指定的元素
const arr5 = [1, 2, 3, 4, 5];
console.log(arr5.includes(3)); // true
console.log(arr5.includes(6)); // false


7. Array.prototype.flat() 将嵌套的数组展开成一维数组

//7. Array.prototype.flat() 将嵌套的数组展开成一维数组
const arr6 = [1, [2, 3], [4, [5, 6]]];
console.log(arr6.flat()); // [1, 2, 3, 4, [5, 6]]


8. Array.prototype.flatMap() 将嵌套的数组展开成一维数组,并对每个元素执行指定的操作

//8. Array.prototype.flatMap() 将嵌套的数组展开成一维数组,并对每个元素执行指定的操作
const arr7 = [1, 2, 3];
const result2 = arr7.flatMap(item => [item * item * 2]);
console.log(result2); // [2, 8, 18]


9. Array.prototype.keys() 返回一个包含数组中每个索引键的迭代器对象

//9. Array.prototype.keys() 返回一个包含数组中每个索引键的迭代器对象
const arr8 = ['a', 'b', 'c'];
const keys = arr8.keys();
for (const key of keys) {
 console.log(key); // 0, 1, 2
}


10. Array.prototype.values() 返回一个包含数组中每个值的迭代器对象

//10. Array.prototype.values() 返回一个包含数组中每个值的迭代器对象
const arr9 = ['a', 'b', 'c'];
const values = arr9.values();
for (const value of values) {
  console.log(value); // 'a', 'b', 'c'
}


11. Array.prototype.entries() 返回一个包含数组中每个索引键值对的迭代器对象

//11. Array.prototype.entries() 返回一个包含数组中每个索引键值对的迭代器对象
const arr10 = ['a', 'b', 'c'];
const entries = arr10.entries();
for (const [index, value] of entries) {
  console.log(index, value); // 0 'a', 1 'b', 2 'c'
}


12. Array.prototype.copyWithin() 将数组中指定位置的元素复制到其他位置

//12. Array.prototype.copyWithin() 将数组中指定位置的元素复制到其他位置
const arr11 = [1, 2, 3, 4, 5];
arr11.copyWithin(0, 3, 4);
console.log(arr11); // [4, 2, 3, 4, 5]


13. Array.prototype.sort() 对数组进行排序

//13. Array.prototype.sort() 对数组进行排序
const arr12 = [3, 1, 4, 2, 5];
arr12.sort();
console.log(arr12); // [1, 2, 3, 4, 5]


14. Array.prototype.reverse() 反转数组中的元素

//14. Array.prototype.reverse() 反转数组中的元素
const arr13 = [1, 2, 3, 4, 5];
arr13.reverse();
console.log(arr13); // [5, 4, 3, 2, 1]


15. Array.prototype.join() 将数组中的元素转换成字符串并连接起来

//15. Array.prototype.join() 将数组中的元素转换成字符串并连接起来
const arr14 = [1, 2, 3, 4, 5];
const str1 = arr14.join('-');
console.log(str1); // '1-2-3-4-5'


16. Array.prototype.concat() 将多个数组合并成一个数组

//16. Array.prototype.concat() 将多个数组合并成一个数组
const arr17. Array.prototype.slice() 返回一个新的数组,包含从开始到结束(不包括结束)的原数组中的元素
const arr16 = [1, 2, 3, 4, 5];
const newArr = arr16.slice(1, 3);
console.log(newArr); // [2, 3]
最近发表
标签列表