Javascript 在树数组模式下添加到最新子级
我有这样一个数组: Javascript代码:Javascript 在树数组模式下添加到最新子级,javascript,Javascript,我有这样一个数组: Javascript代码: var data = [{ name: 'hello', image: '', children: [{ name: 'hello2', image: '', children: [{ name: 'hello3', image: '', children: [{ name: 'hello4', image: '', childr
var data = [{
name: 'hello',
image: '',
children: [{
name: 'hello2',
image: '',
children: [{
name: 'hello3',
image: '',
children: [{
name: 'hello4',
image: '',
children: [{
name: 'hello5',
image: '',
children: []
}]
}]
}]
}]
}];
我想把这个数组添加到最后一个子数组中
{
name: 'hello6',
image: '',
children: []
}
你可以用电脑做类似的事情
var数据=[{
姓名:'你好',
图像:“”,
儿童:[{
名字:“hello2”,
图像:“”,
儿童:[{
姓名:'你好',
图像:“”,
儿童:[{
姓名:'hello4',
图像:“”,
儿童:[{
姓名:'hello5',
图像:“”,
儿童:[]
}]
}]
}]
}]
}];
//变量来存储内部对象引用
var obj=数据[0],
//元素来推动数组
ele={
姓名:'hello6',
图像:“”,
儿童:[]
};
//迭代直至获取空的子数组对象
while(对象子对象长度){
//更新对内部对象的引用
obj=obj.儿童[0];
}
//将其推送到内部对象属性
对象儿童推送(ele);
控制台日志(数据)代码>到目前为止您尝试了什么???也分享期望的结果output@PranavCBalan我以为失败了,你想把它推入内部物体吗?