我试图将一个对象中的所有数组放入一个单独的二维数组中,但是函数的递归性质遇到了麻烦。我能够获得所有的数组,但是按照我编写它的方式,我得到了我不想要的多层嵌套。
下面的代码返回
[
["1-1","1-2","1-3"],
[
["2-1","2-2","2-3"]
],
[
[
["3-1","3-2","3-3"]
]
],
[
[
["4-1-1","4-1-2","4-1-3"],
[
["4-2-1-1","4-2-1-2","4-2-1-3"],
["4-2-2-1","4-2-2-2","4-2-2-3"]
]
]
]
]
无论对象内嵌套如何,如何修改getArrays()
函数以返回二维数组?
function testGetArrays() {
var testObject = {
"one": ["1-1", "1-2", "1-3"],
"two": {
"first": ["2-1", "2-2", "2-3"]
},
"three": {
"first": {
"second": ["3-1", "3-2", "3-3"]
}
},
"four": {
"first": {
"first": ["4-1-1", "4-1-2", "4-1-3"],
"second": {
"first": ["4-2-1-1", "4-2-1-2", "4-2-1-3"],
"second": ["4-2-2-1", "4-2-2-2", "4-2-2-3"]
}
}
}
};
var expectedResult = [
["1-1", "1-2", "1-3"],
["2-1", "2-2", "2-3"],
["3-1", "3-2", "3-3"],
["4-1-1", "4-1-2", "4-1-3"],
["4-2-1-1", "4-2-1-2", "4-2-1-3"],
["4-2-2-1", "4-2-2-2", "4-2-2-3"]
];
var result = getArrays(testObject);
console.log(JSON.stringify(expectedResult) == JSON.stringify(result));
console.log(JSON.stringify(result));
}
function getArrays(object) {
var result = [];
if (Array.isArray(object)) {
if (Array.isArray(object[0])) {
result.push(getArrays(object[0]));
}
result.push(object);
} else {
for (var i in object) {
current = object[i];
if (Array.isArray(current)) {
result.push(current);
} else {
var x = getArrays(current);
result.push(x);
}
}
}
return result;
}
答案 0 :(得分:2)
最简单的调整是为getArrays
提供另一个参数result
,该参数在第一次调用时被初始化为空数组,然后递归传递并进行突变:
testGetArrays();
function testGetArrays() {
var testObject = {
"one": ["1-1", "1-2", "1-3"],
"two": {
"first": ["2-1", "2-2", "2-3"]
},
"three": {
"first": {
"second": ["3-1", "3-2", "3-3"]
}
},
"four": {
"first": {
"first": ["4-1-1", "4-1-2", "4-1-3"],
"second": {
"first": ["4-2-1-1", "4-2-1-2", "4-2-1-3"],
"second": ["4-2-2-1", "4-2-2-2", "4-2-2-3"]
}
}
}
};
var expectedResult = [
["1-1", "1-2", "1-3"],
["2-1", "2-2", "2-3"],
["3-1", "3-2", "3-3"],
["4-1-1", "4-1-2", "4-1-3"],
["4-2-1-1", "4-2-1-2", "4-2-1-3"],
["4-2-2-1", "4-2-2-2", "4-2-2-3"]
];
var result = getArrays(testObject);
console.log(JSON.stringify(expectedResult) == JSON.stringify(result));
console.log(JSON.stringify(result));
console.log(result);
}
function getArrays(object, result = []) {
if (Array.isArray(object)) {
if (Array.isArray(object[0])) {
result.push(getArrays(object[0]));
}
result.push(object);
} else {
for (var i in object) {
current = object[i];
if (Array.isArray(current)) {
result.push(current);
} else {
getArrays(current, result);
}
}
}
return result;
}
答案 1 :(得分:1)
使用递归函数和ES2015:
const collectArrays = function(object){
if (Array.isArray(object)) {
return [object];
}
return Object.keys(object).reduce(function(result, key){
result.push(...collectArrays(object[key]))
return result;
}, []);
}
var testObject = {
"one": ["1-1", "1-2", "1-3"],
"two": {
"first": ["2-1", "2-2", "2-3"]
},
"three": {
"first": {
"second": ["3-1", "3-2", "3-3"]
}
},
"four": {
"first": {
"first": ["4-1-1", "4-1-2", "4-1-3"],
"second": {
"first": ["4-2-1-1", "4-2-1-2", "4-2-1-3"],
"second": ["4-2-2-1", "4-2-2-2", "4-2-2-3"]
}
}
}
};
console.log(collectArrays(testObject))
答案 2 :(得分:1)
创建一个简单的递归函数并检查键的value
。如果它是一个数组,那么如果它是一个对象,则推入一个新数组,然后调用相同的递归函数
var testObject = {
"one": ["1-1", "1-2", "1-3"],
"two": {
"first": ["2-1", "2-2", "2-3"]
},
"three": {
"first": {
"second": ["3-1", "3-2", "3-3"]
}
},
"four": {
"first": {
"first": ["4-1-1", "4-1-2", "4-1-3"],
"second": {
"first": ["4-2-1-1", "4-2-1-2", "4-2-1-3"],
"second": ["4-2-2-1", "4-2-2-2", "4-2-2-3"]
}
}
}
};
let newArr = [];
function getKeys(obj) {
//iterate over the object
for (let keys in obj) {
//check if the value is an array
if (Array.isArray(obj[keys])) {
// if so then push the value to another array
newArr.push(obj[keys])
// if not an array the recall the recursive function
} else if (typeof obj[keys] === 'object') {
getKeys(obj[keys])
}
}
}
getKeys(testObject);
console.log(newArr)