网站首页 > 技术文章 正文
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]
猜你喜欢
- 2024-10-11 js中传统和es6方式 实现数组去重(js数组去重方法es6)
- 2024-10-11 重读《学习JS数据结构与算法-第三版》- 第3章 数组 二
- 2024-10-11 ES6的这些操作技巧,你会吗(es6 教程)
- 2024-10-11 Es6基础语法(es6的语法有哪些)
- 2024-10-11 ES6之扩展运算符的精彩操作(...扩展运算符)
- 2024-10-11 JS面试之数组的几个不low操作(js 数组面试题)
- 2024-10-11 数组的扩展(数组扩展长度的方法)
- 2024-10-11 ES6中Array数组你应该知道的操作(es6数组排序的方法)
- 2024-10-11 ES6 数组的遍历方法最重要的不在代码本身,而是语义化!
- 2024-10-11 ES6 数组解构赋值(es6 结构赋值)
- 最近发表
- 标签列表
-
- cmd/c (90)
- c++中::是什么意思 (84)
- 标签用于 (71)
- 主键只能有一个吗 (77)
- c#console.writeline不显示 (95)
- pythoncase语句 (88)
- es6includes (74)
- sqlset (76)
- apt-getinstall-y (100)
- node_modules怎么生成 (87)
- chromepost (71)
- flexdirection (73)
- c++int转char (80)
- mysqlany_value (79)
- static函数和普通函数 (84)
- el-date-picker开始日期早于结束日期 (76)
- js判断是否是json字符串 (75)
- c语言min函数头文件 (77)
- asynccallback (87)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- 无效的列索引 (74)