插入项目到嵌套的javascript对象中

91zkwejq  于 2022-12-21  发布在  Java
关注(0)|答案(2)|浏览(92)

怎样做一个去关于插入一个项目到对象的一个嵌套javascript数组(有和没有使用一个库)?运行到一个问题那一旦你在遍历之后插入项目,你怎样将它重新分配回到这原始对象而没有手动地访问这对象象data.content[0].content[0].content[0]等..?已经尝试Iterate through Nested JavaScript Objects但是不能得到这重新分配工作

const data = {
    "content": [
        {
            "name": "a",
            "content": [
                {
                    "name": "b",
                    "content": [
                        {
                            "name": "c",
                            "content": []
                        }
                    ]
                }
            ]
        }
    ]
}

{"name": "d", "content": []}插入到c的内容中

const data = {
    "content": [
        {
            "name": "a",
            "content": [
                {
                    "name": "b",
                    "content": [
                        {
                            "name": "c",
                            "content": [{"name": "d", "content": []}]
                        }
                    ]
                }
            ]
        }
    ]
}
jdzmm42g

jdzmm42g1#

const data = {
  "content": [{
    "name": "a",
    "content": [{
      "name": "b",
      "content": [{
        "name": "c",
        "content": []
      }]
    }]
  }]
}

const insert = createInsert(data)

insert({
  "name": "d",
  "content": []
}, 'c')

console.log(data)

// create a new function that will be able to insert items to the object
function createInsert(object) {
  return function insert(obj, to) {
    // create a queue with root data object
    const queue = [object]
    // while there are elements in the queue
    while (queue.length) {
      // remove first element from the queue
      const current = queue.shift()
      // if name of the element is the searched one
      if (current.name === to) {
        // push the object into the current element and break the loop
        current.content.push(obj)
        break
      }
      // add child elements to the queue
      for (const item of current.content) {
        queue.push(item)
      }
    }
  }
}
mxg2im7a

mxg2im7a2#

看起来我们应该假设name属性唯一地标识数据结构中的一个对象,在这个假设下,你可以为它创建一个Map对象,以便将一个给定的名称Map到嵌套结构中对应的对象,还要跟踪哪个对象是给定对象的父对象。
所有这些 meta数据都可以被 Package 在一个装饰器函数中,这样数据对象就可以从它那里获得getaddremove某些名称的一些功能,而不管它在层次结构中的什么位置:

function mappable(data) {
    const map = { "__root__": { content: [] } };
    const parent = {};
    const dfs = (parentName, obj) => {
        parent[obj.name] = parentName;
        map[obj.name] = obj;
        obj.content?.forEach?.(child => dfs(obj.name, child));
    }
    
    Object.defineProperties(data, {
        get: { value(name) {
             return map[name];
        }},
        add: { value(parentName, obj) {
            this.get(parentName).content.push(obj);
            dfs(parentName, obj);
        }},
        remove: { value(name) {
            map[parent[name]].content = map[parent[name]].content.filter(obj =>
                obj.name != name
            );
            delete map[name];
            delete parent[name];
        }}
    });
    data.add("__root__", data);
}

// Demo
const data = {"content": [{"name": "a","content": [{"name": "b","content": [{"name": "c","content": []}]}]}]};

mappable(data);
data.add("c", { name: "d", content: [] });
console.log(data);
console.log(data.get("d")); // { name: "d", content: [] }
data.remove("d");
console.log(data.get("d")); // undefined
console.log(data); // original object structure

相关问题