将递归 json 对象转换为另一个 json 对象
Transform a recursive json object to another json object
需要帮助将递归 json 对象转换为另一个 json 对象。
我的输入对象如下所示:
{
person:
{
name: 'John Henry',
age: 63
},
children:
[
{
person:
{
name: 'Paul Henry',
age: 40
},
children:
[
{
person:
{
name: 'Tom Henry',
age: 10
}
}
{
person:
{
name: 'Mike Henry',
age: 12
}
}
]
},
{
person:
{
name: 'Wilson Henry',
age: 30
}
},
{
person:
{
name: 'Richard Henry',
age: 59
}
}
]
}
我想要的输出是:
[{
name: 'John Henry',
attributes: { age: 63 },
children:
[
{
name: 'Paul Henry',
attributes: { age: 40 },
children: [
{
name: 'Tom Henry',
attributes: { age: 10 }
},
{
name: 'Mike Henry',
attributes: { age: 12 }
}
]
},
{
name: 'Wilson Henry',
attributes: { age: 30 }
},
{
name: 'Richard Henry',
attributes: { age: 59 }
}
]
}];
这是我目前尝试过的方法,但卡在了递归部分。不确定如何将所有内容组合在一起。:
var tree = {};
var getFamilyTree = function (input) {
tree.name = input.person.name;
tree.attributes = { 'age': input.person.age };
tree.children = [];
input.children.forEach(function (child) {
//not sure how to recursively go through the child nodes.
});
};
在函数的开头创建一个新输出 object。
对于每个 child,在 child 上再次调用 getFamilyTree()
- 这将是新的 input
。将调用的结果附加到新的 children 列表中。
Return 你的 newly-created object.
类似于:
function getFamilyTree(input)
{
var newb = { 'attributes': {} };
for ( f in input.person ) // we may have attributes other than "age"
if (f == 'name')
newb.name = input.person[f];
else
newb.attributes[f] = input.person[f];
if (input.children && input.children.length)
{
newb.children = [];
for ( var i = 0; i < input.children.length; ++i )
{
newb.children.push(getFamilyTree(input.children[i]));
}
}
return newb;
}
var orig = {
person:
{
name: 'John Henry',
age: 63
},
children:
[
{
person:
{
name: 'Paul Henry',
age: 40
},
children:
[
{
person:
{
name: 'Tom Henry',
age: 10
}
},
{
person:
{
name: 'Mike Henry',
age: 12
}
}
]
},
{
person:
{
name: 'Wilson Henry',
age: 30
}
},
{
person:
{
name: 'Richard Henry',
age: 59
}
}
]
};
function getFamilyTree(o)
{
var newb = { 'attributes': {} };
for ( f in o.person ) // we may have attributes other than "age"
if (f == 'name')
newb.name = o.person[f];
else
newb.attributes[f] = o.person[f];
if (o.children && o.children.length)
{
newb.children = [];
for ( var i = 0; i < o.children.length; ++i )
{
newb.children.push(getFamilyTree(o.children[i]));
}
}
return newb;
}
console.log( JSON.stringify( getFamilyTree(orig), null, " " ) );
需要帮助将递归 json 对象转换为另一个 json 对象。
我的输入对象如下所示:
{
person:
{
name: 'John Henry',
age: 63
},
children:
[
{
person:
{
name: 'Paul Henry',
age: 40
},
children:
[
{
person:
{
name: 'Tom Henry',
age: 10
}
}
{
person:
{
name: 'Mike Henry',
age: 12
}
}
]
},
{
person:
{
name: 'Wilson Henry',
age: 30
}
},
{
person:
{
name: 'Richard Henry',
age: 59
}
}
]
}
我想要的输出是:
[{
name: 'John Henry',
attributes: { age: 63 },
children:
[
{
name: 'Paul Henry',
attributes: { age: 40 },
children: [
{
name: 'Tom Henry',
attributes: { age: 10 }
},
{
name: 'Mike Henry',
attributes: { age: 12 }
}
]
},
{
name: 'Wilson Henry',
attributes: { age: 30 }
},
{
name: 'Richard Henry',
attributes: { age: 59 }
}
]
}];
这是我目前尝试过的方法,但卡在了递归部分。不确定如何将所有内容组合在一起。:
var tree = {};
var getFamilyTree = function (input) {
tree.name = input.person.name;
tree.attributes = { 'age': input.person.age };
tree.children = [];
input.children.forEach(function (child) {
//not sure how to recursively go through the child nodes.
});
};
在函数的开头创建一个新输出 object。
对于每个 child,在 child 上再次调用 getFamilyTree()
- 这将是新的 input
。将调用的结果附加到新的 children 列表中。
Return 你的 newly-created object.
类似于:
function getFamilyTree(input)
{
var newb = { 'attributes': {} };
for ( f in input.person ) // we may have attributes other than "age"
if (f == 'name')
newb.name = input.person[f];
else
newb.attributes[f] = input.person[f];
if (input.children && input.children.length)
{
newb.children = [];
for ( var i = 0; i < input.children.length; ++i )
{
newb.children.push(getFamilyTree(input.children[i]));
}
}
return newb;
}
var orig = {
person:
{
name: 'John Henry',
age: 63
},
children:
[
{
person:
{
name: 'Paul Henry',
age: 40
},
children:
[
{
person:
{
name: 'Tom Henry',
age: 10
}
},
{
person:
{
name: 'Mike Henry',
age: 12
}
}
]
},
{
person:
{
name: 'Wilson Henry',
age: 30
}
},
{
person:
{
name: 'Richard Henry',
age: 59
}
}
]
};
function getFamilyTree(o)
{
var newb = { 'attributes': {} };
for ( f in o.person ) // we may have attributes other than "age"
if (f == 'name')
newb.name = o.person[f];
else
newb.attributes[f] = o.person[f];
if (o.children && o.children.length)
{
newb.children = [];
for ( var i = 0; i < o.children.length; ++i )
{
newb.children.push(getFamilyTree(o.children[i]));
}
}
return newb;
}
console.log( JSON.stringify( getFamilyTree(orig), null, " " ) );