(一)使用 length 调整数组大小- let array = [11, 12, 13, 14, 15];
- console.log(array.length); // 5
- array.length = 3;
- console.log(array.length); // 3
- console.log(array); // [11,12,13]
- array.length = 0;
- console.log(array.length); // 0
- console.log(array); // []
复制代码
(二)使用数组解构来交换值
- 解构赋值语法是一种 JavaScript 表达式,可以将数组中的值或对象中的属性解包为不同的变量
- let a = 1, b = 2
- [a, b] = [b, a]
- console.log(a) // -> 2
- console.log(b) // -> 1
复制代码
(三)随机排列数组中的元素
- let list = [1, 2, 3, 4, 5, 6, 7, 8, 9];
- console.log(list.sort(function() {
- return Math.random() - 0.5
- }));
- // [4, 8, 2, 9, 1, 3, 6, 5, 7]
复制代码
(四)属性名称可以是动态的
- const dynamic = 'color';
- let item = {
- brand: 'Ford',
- [dynamic]: 'Blue'
- }
- console.log(item);
- // { brand: "Ford", color: "Blue" }
复制代码
(五)过滤唯一值
- 对于所有 ES6 爱好者而言,我们可以使用带有 Spread 运算符的 Set 对象来创建一个仅包含唯一值的新数组
- const my_array = [1, 2, 2, 3, 3, 4, 5, 5]
- const unique_array = [...new Set(my_array)];
- console.log(unique_array); // [1, 2, 3, 4, 5]
复制代码 总结:
履行好自己的责任比提升效率要重要的多。 你的网站需要兼容所有浏览器。
|
|