简体   繁体   English

通过属性比较对象数组,然后 append 数组中的新 object:Javascript

[英]Compare array of objects by a property and then append the new object inside the array: Javascript

I have two array of objects, where in the name in these needs to be compared and get the values and append it to one of the array.我有两个对象数组,其中需要比较这些对象的name并将值和 append 放入数组之一。 I was able to do it for the first level, How can I achieve the same recursively and update the object.我能够在第一级做到这一点,我怎样才能递归地实现相同的目标并更新 object。

const arr1 = [
  {
    name: "internalcorp.com",
    children: [
      {
        name: "internalcorp.com.child1",
        children: [
          {
            name: "internalcorp.com.grandchild1",
            children: []
          },
          {
            name: "internalcorp.com.grandchild2",
            children: []
          }
        ]
      },
      {
        name: "internalcorp.com.child2",
        children: []
      }
    ]
  },
  {
    name: "internalcorpwebsite.com",
    children: [
      {
        name: "internalcorpwebsite.com.child1",
        children: []
      }
    ]
  }
];

const arr2 = [
  {
    name: "internalcorp.com",
    val1: false,
    val2: false
  },
  {
    name: "internalcorp.com.child1",
    val1: true,
    val2: true
  },
  {
    name: "internalcorp.com.grandchild1",
    val1: true,
    val2: true
  },
  {
    name: "internalcorp.com.grandchild2",
    val1: false,
    val2: true
  },
  {
    name: "internalcorp.com.child2",
    val1: true,
    val2: false
  },
  {
    name: "internalcorpwebsite.com",
    val1: false,
    val2: false
  },
  {
    name: "internalcorpwebsite.com.child1",
    val1: false,
    val2: false
  }
];

Output should look like Output 应该是这样的

const res = [
  {
    name: "internalcorp.com",
    config: {
      val1: false,
      val2: false
    },
    children: [
      {
        name: "internalcorp.com.child1",
        config: {
          val1: true,
          val2: true
        },
        children: [
          {
            name: "internalcorp.com.grandchild1",
            config: {
              val1: true,
              val2: true
            },
            children: []
          },
          {
            name: "internalcorp.com.grandchild2",
            config: {
              val1: false,
              val2: true
            },
            children: []
          }
        ]
      },
      {
        name: "internalcorp.com.child2",
        config: {
          val1: true,
          val2: false
        },
        children: []
      }
    ]
  },
  {
    name: "internalcorpwebsite.com",
    children: [
      {
        name: "internalcorpwebsite.com.child1",
        className: "level-1 leaf",
        children: [],
        val1: false,
        val2: false
      }
    ],
    config: {
      val1: false,
      val2: false
    }
  }
];

Code that I tried我试过的代码

  const result = arr1.map((item) => {
    let config = arr2.find((p) => p.name === item.name);
    return {
      ...item,
      config: {
        val1: config.val1,
        val2: config.val2
      }
    };
  });

You're very close What to do:你非常接近怎么办:

  1. Wrap your .map() in a named function so it can be called recursively (I went with addConfig )将您的.map()包装在命名为 function 中,以便可以递归调用它(我使用addConfig
  2. In the return object of .map() , add the key/value pair children: addConfig(item.children).map()的返回 object 中,添加键/值对children: addConfig(item.children)

That'll do the trick.那会成功的。

 const arr1 = [ { name: "internalcorp.com", children: [ { name: "internalcorp.com.child1", children: [ { name: "internalcorp.com.grandchild1", children: [] }, { name: "internalcorp.com.grandchild2", children: [] } ] }, { name: "internalcorp.com.child2", children: [] } ] }, { name: "internalcorpwebsite.com", children: [ { name: "internalcorpwebsite.com.child1", children: [] } ] } ]; const arr2 = [ { name: "internalcorp.com", val1: false, val2: false }, { name: "internalcorp.com.child1", val1: true, val2: true }, { name: "internalcorp.com.grandchild1", val1: true, val2: true }, { name: "internalcorp.com.grandchild2", val1: false, val2: true }, { name: "internalcorp.com.child2", val1: true, val2: false }, { name: "internalcorpwebsite.com", val1: false, val2: false }, { name: "internalcorpwebsite.com.child1", val1: false, val2: false } ]; const addConfig = (items) => items.map(item => { const { val1, val2 } = arr2.find(p => p.name === item.name); return {...item, config: { val1, val2 }, children: addConfig(item.children) }; }); const result = addConfig(arr1); console.log(result);

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

相关问题 JavaScript 将新对象附加到对象数组 - JavaScript Append a new object to an array of objects Javascript - 比较两个数组对象和 append 值 - Javascript - Compare two array objects and append values 在另一个数组内的JavaScript对象数组中按属性查找对象 - Find object by property in an array of JavaScript objects inside another array Javascript中新对象数组中数组对象属性值的总和 - Sum of array object property values in new array of objects in Javascript 比较2个对象以在javascript中创建对象的新数组 - Compare 2 objects to create a new array of objects in javascript 如何比较数组中的值,即对象数组中的值与对象的属性? - How to compare values in an array, that is inside an array of objects, with a property of the objects? Javascript - 如何比较数组中对象的属性值 - Javascript - How to compare property values in objects in an array 按属性将对象数组与另一个对象的键进行比较 - Compare array of objects by property with key of another object 在对象属性下的对象数组内搜索JavaScript中的字符串帮助 - Search inside an array of objects under an object property for a string assistance in JavaScript Javascript:如何为对象数组中的 object 属性设置值? - Javascript: How to set value to an object property inside an array of objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM