简体   繁体   English

如何在数组中找到具有相同键值的对象?

[英]How can I find objects with same keys values in array?

I have an array of objects that looks like this:我有一个看起来像这样的对象数组:

  const arr = [
    { type: 'type', fields: ['field1'] },
    { type: 'type2' },
    { type: 'type', fields: ['field2'] },
  ]

And I need to find objects with the same type to merge fields key in them, like this:我需要找到具有相同类型的对象来合并其中的字段键,如下所示:

  const arr = [
    { type: 'type', fields: ['field1', 'field2'] },
    { type: 'type2' },
    { type: 'type', fields: ['field1', 'field2'] },
  ]

My plan was to filter through the array, but my problem is that I don't know which type will send me API, so filtering by item.type wouldn't work for me.我的计划是过滤数组,但我的问题是我不知道哪种类型会发送给我 API,所以按item.type过滤对我不起作用。

If that is the exact solution that you want.如果那是您想要的确切解决方案。 Following code snippet may help you.以下代码段可能会对您有所帮助。

    const arr = [
      { type: 'type', fields: ['field1']},
      { type: 'type2'},
      { type: 'type', fields: ['field2']}
    ]
    
    const modifyArr = (data) => {
      let res = [];
      arr.map((item) => {
          if(item.type == data.type){
            if(Object.keys(item).includes('fields')){
              res = res.concat(item.fields);
            }
          }
      });
      return Object.keys(data).includes('fields') ? { type: data.type, fields: res } : { type: data.type };

}

let newArr = arr.map(item => modifyArr(item));

console.log(newArr); 

This will print这将打印

[
    { type: 'type', fields: ['field1', 'field2'] },
    { type: 'type2' },
    { type: 'type', fields: ['field1', 'field2'] },
  ]

 const arr = [{ type: 'type', fields: ['field1']}, { type: 'type2'}, { type: 'type', fields: ['field2']}]; result = arr.map(({ type }) => { const fields = arr.filter((o) => o.type === type).flatMap((e) => e.fields); return { type, ...fields[0]? { fields }: {} }; }); console.log(result); //[ // {"type": "type", "fields": ["field1", "field2"]}, // {"type": "type2"}, // {"type": "type", "fields": ["field1", "field2"]} //]

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

相关问题 如何将对象数组转换为具有相同值但具有修改键的对象数组? - How can I convert an array of objects into an array of objects with the same values but with modified keys? 如何在对象中找到相同的键并计算值 - How do I find same keys in objects and calculate the values 如何将具有相同键的对象数组合到一个对象中? - How can I combine an array of objects with the same keys into one object? 如何在对象数组中获取具有唯一值的键? (JavaScript) - How can I get keys with unique values in an array of objects? (JavaScript) 如何在所有对象的键相同且值可能不同的情况下将对象数组转换为单个 object - How can I convert an array of objects into single object while keys of all the objects are same and value may differ 如何检查对象数组是否具有相同的值 - How can I check if the array of objects have same values 如何在对象数组中查找和更新值? - How can I find and update values in an array of objects? 如何检查数组中的所有对象是否包含相同的键和值? - How to check if all objects in an array contains same keys and values? 如何在对象数组中找到一个对象的所有匹配键和值? - How to find all matching keys and values of one object in array of objects? 如何从数组中的多个对象中准确提取 2 个键和值 - How can I extract exactly 2 keys and values from several objects in an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM