简体   繁体   English

更新基于 object 的深度嵌套对象数组:Javascript

[英]Update an deeply nested array of objects based on an object : Javascript

I am having an deeply nested array of objects with certain properties, and given an object as input, it should update that object along with its children recursively.我有一个具有特定属性的深度嵌套对象数组,并且给定 object 作为输入,它应该递归地更新 object 及其子对象。 The format it has is as follows,它的格式如下,

const arr = [
  {
    name: "parent",
    children: [
      {
        name: "child1",
        children: [
          {
            name: "granchild1",
            children: [],
            class: "level-2 leaf",
            config: {
              name: "granchild1",
              value1: false,
              value2: false
            }
          }
        ],
        class: "level-1 leaf",
        config: {
          name: "child1",
          value1: false,
          value2: false
        }
      },
      {
        name: "child2",
        children: [],
        class: "level-1 leaf",
        config: {
          name: "child2",
          value1: false,
          value2: false
        }
      }
    ],
    class: "level-0 group",
    config: {
      name: "parent",
      value1: false,
      value2: false
    }
  }
];

The given input object will look like给定的输入 object 看起来像

const obj = {
  name: "parent",
  value1: true,
  value2: true
};

Given this input, the object with matching name should update it's and children's value1 and value2 with the obj's values鉴于此输入,具有匹配名称的 object 应该使用obj's值更新它和孩子的value1value2

Output should look like Output 应该是这样的

const result = [
  {
    name: "parent",
    children: [
      {
        name: "child1",
        children: [
          {
            name: "granchild1",
            children: [],
            class: "level-2 leaf",
            config: {
              name: "granchild1",
              value1: true,
              value2: true
            }
          }
        ],
        class: "level-1 leaf",
        config: {
          name: "child1",
          value1: true,
          value2: true
        }
      },
      {
        name: "child2",
        children: [],
        class: "level-1 leaf",
        config: {
          name: "child2",
          value1: true,
          value2: true
        }
      }
    ],
    class: "level-0 group",
    config: {
      name: "parent",
      value1: true,
      value2: true
    }
  }
];

Code that I tried.我试过的代码。 How do I achieve the same output我如何实现相同的 output

const res = arr.map((item) => {
  let foundItem = arr.find((item) => item.name === obj.name);
  return {
    ...foundItem,
    children: {
      value1: obj.value1,
      value2: obj.value2
    }
  };
});

You need a recursive function that passes along whether the desired parent was found in one of its ancestors.您需要一个递归的 function 来传递是否在其祖先之一中找到了所需的父级。

 const arr=[{name:"parent",children:[{name:"child1",children:[{name:"granchild1",children:[],class:"level-2 leaf",config:{name:"granchild1",value1:,1:value2,:1}}],class:"level-1 leaf":config,{name:"child1",value1:,1:value2,:1}},{name:"child2",children:[]:class,"level-1 leaf":config,{name:"child2",value1:,1:value2:,1}}]:class,"level-0 group":config;{name,"parent",value1,.1.value2;?1}}]: const recurse = (arr. nameToFind. objToMerge. inAncestor = false) => { return arr,map(obj => { const mergeThis = inAncestor || obj:name === nameToFind. const merged =.mergeThis. obj. {,..obj. config; {...obj,config, ,;;objToMerge } }; if (merged;children) { merged:children = recurse(merged,children: nameToFind, objToMerge: mergeThis); } return merged, }). }. const obj = { name. "parent"; value1, true, value2; true }. const { name; ...objToMerge } = obj; const result = recurse(arr, name, objToMerge); console.log(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM