<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var data = [
[{
name: "a",
age: 12
}, {
name: "b",
age: 11
}], {
name: "c",
age: 13
}, {
name: "d",
age: 14
},
[
[{
name: "e"
}, {
name: "f"
}], {
name: "g"
}
]
];
function isArray(obj) { // 判断是否是数组类型
return Object.prototype.toString.call(obj) === '[object Array]';
}
let newArr = []
function test(source) {
for (var i in source) {
var item = source[i];
// if (item instanceof Array) {
if (isArray(item)) {
test(item);
} else {
// console.log(item);
newArr.push(item)
}
}
}
test(data);
console.log(newArr);
</script>
</body>
</html>
利用递归遍历多维数组
最新推荐文章于 2024-06-26 11:24:48 发布