有一个这样的数据结构。{
"attrTree": [{
"attrValueCode": "VI10001001",
"children": [{
"attrValueCode": "VI10002001",
"children": [{
"attrValueCode": "VI10003001",
}]
},
{
"attrValueCode": "VI10002002",
"children": [{
"attrValueCode": "VI10003001",
}]
},
{
"attrValueCode": "VI10002003",
"children": [{
"attrValueCode": "VI10003001",
}]
}
]
},
{
"attrValueCode": "VI10001002",
"children": [{
"attrValueCode": "VI10002001",
"children": [{
"attrValueCode": "VI10003001",
}]
}]
}
]
};我想用递归去求出所有attrValueCode的结果。
自己尝试无果。正确返回结果应为。
[VI10001001, VI10002001, VI10003001]
[VI10001001, VI10002002, VI10003001]
[VI10001001, VI10002003, VI10003001]
[VI10001002, VI10002001, VI10003001]请教下思路。
这里是我的代码
var array = [];
function getCode(value,isFirst){
if (isFirst) {
for (var i = 0; i < value.attrTree.length; i++) {
array.push(value.attrTree.attrValueCode)
getCode(value.attrTree,false);
}
} else if (value.hasOwnProperty('children')){
for (var i = 0; i < value.children.length; i++) {
array.push(value.children.attrValueCode)
getCode(value.children,false);
}
} else {
console.log('array',array);
}
}==========================贴自己代码的时候发现问题所在了..但是还是想看看大神的解法
"attrTree": [{
"attrValueCode": "VI10001001",
"children": [{
"attrValueCode": "VI10002001",
"children": [{
"attrValueCode": "VI10003001",
}]
},
{
"attrValueCode": "VI10002002",
"children": [{
"attrValueCode": "VI10003001",
}]
},
{
"attrValueCode": "VI10002003",
"children": [{
"attrValueCode": "VI10003001",
}]
}
]
},
{
"attrValueCode": "VI10001002",
"children": [{
"attrValueCode": "VI10002001",
"children": [{
"attrValueCode": "VI10003001",
}]
}]
}
]
};我想用递归去求出所有attrValueCode的结果。
自己尝试无果。正确返回结果应为。
[VI10001001, VI10002001, VI10003001]
[VI10001001, VI10002002, VI10003001]
[VI10001001, VI10002003, VI10003001]
[VI10001002, VI10002001, VI10003001]请教下思路。
这里是我的代码
var array = [];
function getCode(value,isFirst){
if (isFirst) {
for (var i = 0; i < value.attrTree.length; i++) {
array.push(value.attrTree.attrValueCode)
getCode(value.attrTree,false);
}
} else if (value.hasOwnProperty('children')){
for (var i = 0; i < value.children.length; i++) {
array.push(value.children.attrValueCode)
getCode(value.children,false);
}
} else {
console.log('array',array);
}
}==========================贴自己代码的时候发现问题所在了..但是还是想看看大神的解法
"attrTree": [{
"attrValueCode": "VI10001001",
"children": [{
"attrValueCode": "VI10002001",
"children": [{
"attrValueCode": "VI10003001",
}]
}, {
"attrValueCode": "VI10002002",
"children": [{
"attrValueCode": "VI10003001",
}]
}, {
"attrValueCode": "VI10002003",
"children": [{
"attrValueCode": "VI10003001",
}]
}]
}, {
"attrValueCode": "VI10001002",
"children": [{
"attrValueCode": "VI10002001",
"children": [{
"attrValueCode": "VI10003001",
}]
}]
}]
};var array = [];function getCode(value) {
for (var i = 0; i < value.length; i++) {
array.push(value[i].attrValueCode);
if (value[i].hasOwnProperty('children'))
getCode(value[i].children);
}
}
getCode(data.attrTree);
alert(array);
"attrTree": [{
"attrValueCode": "VI10001001",
"children": [{
"attrValueCode": "VI10002001",
"children": [{
"attrValueCode": "VI10003001",
}]
}, {
"attrValueCode": "VI10002002",
"children": [{
"attrValueCode": "VI10003001",
}]
}, {
"attrValueCode": "VI10002003",
"children": [{
"attrValueCode": "VI10003001",
}]
}]
}, {
"attrValueCode": "VI10001002",
"children": [{
"attrValueCode": "VI10002001",
"children": [{
"attrValueCode": "VI10003001",
}]
}]
}]
};var array = [];function getCode(value) {
for (var i = 0; i < value.length; i++) {
array.push(value[i].attrValueCode);
if (value[i].hasOwnProperty('children'))
getCode(value[i].children);
}
}
getCode(data.attrTree);
alert(array);
function getAttrVlaueCode(data,arr) {
for(let i in data) {
arr.push(data[i].attrValueCode);
if(data[i].hasOwnProperty('children')) getAttrVlaueCode(data[i].children,arr);
}
return arr;
} console.log(getAttrVlaueCode(data.attrTree,[]))
"attrTree": [{
"attrValueCode": "VI10001001",
"children": [{
"attrValueCode": "VI10002001",
"children": [{
"attrValueCode": "VI10003001",
}]
},
{
"attrValueCode": "VI10002002",
"children": [{
"attrValueCode": "VI10003001",
}]
},
{
"attrValueCode": "VI10002003",
"children": [{
"attrValueCode": "VI10003001",
}]
}
]
},
{
"attrValueCode": "VI10001002",
"children": [{
"attrValueCode": "VI10002001",
"children": [{
"attrValueCode": "VI10003001",
}]
}]
}
]
}
const array = []
function getAttrValueCode(data,arr) {
data.forEach(obj => {
let a = arr ? arr.slice(0) : []
a.push(obj.attrValueCode)
Array.isArray(obj.children) ? getAttrValueCode(obj.children, a) : array.push(a.slice(0))
})
}
getAttrValueCode(data.attrTree)
console.log(JSON.stringify(array,null,' '))
var getCode = function(value){
var temp = array[array.length];
for(var i=0; i<value.length; i++){
temp && i && array.push(temp);
array.push(value[i].attrValue Code);
value[i].children && getCode(value[i].children);
}
}
getCode(data.attrTree);