json 如何在JavaScript对象中删除/插入元素?[closed]

mrphzbgm  于 2022-11-19  发布在  Java
关注(0)|答案(1)|浏览(114)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
2天前关闭。
Improve this question
如何从JavaScript对象中删除一个元素并将其插入到另一个位置(作为子元素,请参见insert element here)我正在按id递归搜索元素
所有对象:

let myObject = {
  id: '54321dfg77',
  type: 'newURI2',
  data: [],
  children: [
    {
      id: 'fgh41dfg77',
      type: 'newURI34',
      data: [],
      children: [
        {
          id: 'tyu15dfg77',
          type: 'newURI789',
          data: [],
          children: [
            {
              id: 'ty1dfg745',
              type: 'newURI7589',
              data: [],
              children: [],
            },
            **<<insert element here>>**
          ],
        },
        {
          id: 'fyruj994l',
          type: 'newURI243',
          data: [],
          children: [],
        },
      ],
    },
    {
      id: '28u1dfg76',
      type: 'newURI1129',
      data: [],
      children: [
        {
          id: 'tyu1rty200',
          type: 'newURI489',
          data: [],
          children: [
            {
              id: 'ryyu1dfg66',
              type: 'newURI339',
              data: [],
              children: [],
            },
          ],
        },
        {
          id: '1dfg990',
          type: 'newURI72',
          data: [],
          children: [],
        },
      ],
    },
  ]
};

要素:

{
  id: 'tyu1rty200',
  type: 'newURI489',
  data: [],
  children: [
    {
      id: 'ryyu1dfg66',
      type: 'newURI339',
      data: [],
      children: [],
    },
  ],
},

应将Element插入到id为的子数组中:'tyu15dfg77' ....................................

n7taea2i

n7taea2i1#

像这样吗?

const myObject = {
    id: '54321dfg77',
    type: 'newURI2',
    data: [],
    children: [
        {
            id: 'fgh41dfg77',
            type: 'newURI34',
            data: [],
            children: [
                {
                    id: 'tyu15dfg77',
                    type: 'newURI789',
                    data: [],
                    children: [
                        {
                            id: 'ty1dfg745',
                            type: 'newURI7589',
                            data: [],
                            children: [],
                        }
                    ],
                },
                {
                    id: 'fyruj994l',
                    type: 'newURI243',
                    data: [],
                    children: [],
                },
            ],
        },
        {
            id: '28u1dfg76',
            type: 'newURI1129',
            data: [],
            children: [
                {
                    id: 'tyu1rty200',
                    type: 'newURI489',
                    data: [],
                    children: [
                        {
                            id: 'ryyu1dfg66',
                            type: 'newURI339',
                            data: [],
                            children: [],
                        },
                    ],
                },
                {
                    id: '1dfg990',
                    type: 'newURI72',
                    data: [],
                    children: [],
                },
            ],
        },
    ]
};

const addElToChildrenById = (obj = {}, id = "", el) => {
    if (obj.id === id) {
        obj.children ??= [];
        obj.children.push(el);
        return;
    }
    if (obj.children && obj.children.length > 0) {
        obj.children.forEach(oc => addElToChildrenById(oc, id, el));
    }
}

const newObj = {
    id: 'tyu1rty200',
    type: 'newURI489',
    data: [],
    children: [
        {
            id: 'ryyu1dfg66',
            type: 'newURI339',
            data: [],
            children: [],
        },
    ],
};

addElToChildrenById(myObject, "tyu15dfg77", newObj);

console.log("result", myObject);

相关问题