Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在树数组模式下添加到最新子级_Javascript - Fatal编程技术网

Javascript 在树数组模式下添加到最新子级

Javascript 在树数组模式下添加到最新子级,javascript,Javascript,我有这样一个数组: Javascript代码: var data = [{ name: 'hello', image: '', children: [{ name: 'hello2', image: '', children: [{ name: 'hello3', image: '', children: [{ name: 'hello4', image: '', childr

我有这样一个数组:

Javascript代码:

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我以为失败了,你想把它推入内部物体吗?