利用函数递归将含有对象格式的多维数组转化为一维数组并保留元素的对象格式
例如:将多维数组["1", "2", ["3", ["4", {old: "23"}, "5"], "6"]]转化成["1","2","3","4",{old:"23"},"5","6"]
var a = ["1", "2", ["3", ["4", {old: "23"}, "5"], "6"]]; var newArr = []; function fun(arr){ for(var i=0;i<arr.length;i++){ if(Array.isArray(arr[i])){ fun(arr[i]); }else{ newArr.push(arr[i]); } } } fun(a); console.log(newArr) // ["1","2","3","4",{old:"23"},"5","6"]