一、 数组的深度遍历:array.flat(depth) 嵌套数组转一维数组 ES6
console.log([1 ,[2, 3]].flat()); // [1, 2, 3]
// 指定转换的嵌套层数
console.log([1, [2, [3, [4, 5]]]].flat(2)); // [1, 2, 3, [4, 5]]
// 不管嵌套多少层
console.log([1, [2, [3, [4, 5]]]].flat(Infinity)); // [1, 2, 3, 4, 5]
// 自动跳过空位
console.log([1, [2, , 3]].flat());<p> // [1, 2, 3]
详解: https://www.runoob.com/w3cnote/es6-array.html
二、数组的深度map 遍历:flatMap() ES6
先对数组中每个元素进行了的处理,再对数组执行 flat() 方法。
// 参数1:遍历函数,该遍历函数可接受3个参数:当前元素、当前元素索引、原数组
// 参数2:指定遍历函数中 this 的指向
console.log([1, 2, 3].flatMap(n => [n * 2])); // [2, 4, 6]
详解: https://www.runoob.com/w3cnote/es6-array.html
三、json 类型数组 转数组:Array.from() ES6
将类数组对象或可迭代对象转化为数组
// 参数为数组,返回与原数组一样的数组
console.log(Array.from([1, 2])); // [1, 2]
// 参数含空位
console.log(Array.from([1, , 3])); // [1, undefined, 3]
详解: https://www.runoob.com/w3cnote/es6-array.html
四、判断是否是数组:Array.isArray()
如果对象是数组返回 true,否则返回 false。
<body>
<p>点击按钮检测 "fruits" 变量是否为一个数组。</p>
<button onclick="myFunction()">点我</button>
<p id="demo"></p>
<script>
function myFunction() {
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var x = document.getElementById("demo");
x.innerHTML = Array.isArray(fruits);
}
</script>
</body>
菜鸟教程演示:https://www.runoob.com/try/try.php?filename=tryjsref_isarray