【问题标题】:Merge 2 arrays of objects based on different key but same value基于不同的键但相同的值合并2个对象数组
【发布时间】:2019-06-04 16:06:45
【问题描述】:

我已经参考了下面列出的问题,但没有得到相关答案。

  1. How can I merge properties of two JavaScript objects dynamically?
  2. merge two json object based on key value in javascript
  3. Merge two array of objects based on a key

我有 2 个对象数组,

OBJ1 -

[
    {
        "ID": 58895,
        "step": "Outage Agreed w/ Business"
    },
    {
        "ID": 58896,
        "step": "GMLC/E911/CMAS Significant"
    }
]

OBJ2 -

[
    {
        "type": "verification_step",
        "value": "GMLC/E911/CMAS Significant"
    },
    {
        "type": "verification_step",
        "value": "Outage Agreed w/ Business"
    }
]

我希望输出包含基于字符串值的单个对象中的两个值,即

[
    {
        "ID": 58896,
        "type": "verification_step",
        "step": "GMLC/E911/CMAS Significant"
    },
    {
        "ID": 58895,
        "type": "verification_step",
        "step": "Outage Agreed w/ Business"
    }
]

请给我建议出路。 (ES6 解决方案 - 非常感谢)

编辑

第三个被指定为重复的参考不是场景。键应保持“step”,数据应合并。

【问题讨论】:

标签: javascript arrays object array-merge


【解决方案1】:

合并两个数组后可以使用reduce()

const arr1 = [ { "ID": 58895, "step": "Outage Agreed w/ Business" }, { "ID": 58896, "step": "GMLC/E911/CMAS Significant" } ] 

const arr2 = [ { "type": "verification_step", "value": "GMLC/E911/CMAS Significant" }, { "type": "verification_step", "value": "Outage Agreed w/ Business" } ]

const res = [...arr1,...arr2.map(({value,...rest}) => ({step:value,...rest}))].reduce((ac,a) => {
  let k = a.step;
  ac[k] = {...ac[k],...a} || a;
  return ac;
},{})
console.log(Object.values(res))

【讨论】:

  • 是否有可能关键仍然是“步骤”而不是“价值”?
  • @KeyurK 请添加预期的输出。
  • 编辑了预期的输出
  • 这不是我想要的Maheer。我想比较字符串然后合并数据。我不仅有要合并的 ID。
  • @KeyurK 我知道。它将适用于更多属性。我给出了正确的输出,我猜你已经发布了。你能告诉它失败的地方吗?
【解决方案2】:

您可能可以将其作为一个单行来执行,但为了可读性和效率,根据您想要从一个数组中的值创建一个查找对象,然后使用查找 map 另一个数组可能会更好加入你想要的其他价值。比如:

let arr1 = [{"ID": 58895,"step": "Outage Agreed w/ Business"},{"ID": 58896,"step": "GMLC/E911/CMAS Significant"}]
let arr2 = [{"type": "verification_step","value": "GMLC/E911/CMAS Significant"},{"type": "verification_step","value": "Outage Agreed w/ Business"}]

// lookup based on value
let lookup = arr2.reduce((m, {type, value}) => m.set(value, {type}), new Map)

// merge each item based on lookup
let result = arr1.map(item =>  Object.assign({}, item, lookup.get(item.step)))

console.log(result)

【讨论】:

    【解决方案3】:

    你可以使用lodash:

    const _ = require('lodash');
    const arr1 = [
        {
            "ID": 58895,
            "step": "Outage Agreed w/ Business"
        },
        {
            "ID": 58896,
            "step": "GMLC/E911/CMAS Significant"
        }
    ]
    
    const arr2 = [
        {
            "type": "verification_step",
            "value": "GMLC/E911/CMAS Significant"
        },
        {
            "type": "verification_step",
            "value": "Outage Agreed w/ Business"
        }
    ]
    const res =  _(arr1)
        .keyBy('step')
        .merge((_.keyBy(arr2, 'value')))
        .values()
        .map((value) => {
          const { ID, type, step } = value
          return {
             ID, 
             type, 
             step
          }
        })
        .value()
    

    【讨论】:

      【解决方案4】:

      你可以写 这是测试链接online es6 compiler

      let arr = [];
       obj1.map((val, index) => {
        if(obj2[index]) {
         arr.push({ id: val.ID, type: obj2[index].type, value: obj2[index].value })
        }
      });
      
      console.log(arr);
      

      【讨论】:

        【解决方案5】:

        因为你需要 ES6,你可以只使用 map 操作符并将缺失的值连接到对象上。 像这样

        let obj=[
            {
                "type": "verification_step",
                "value": "GMLC/E911/CMAS Significant"
            },
            {
                "type": "verification_step",
                "value": "Outage Agreed w/ Business"
            }
        ]
        
        const obj1=[
            {
                "ID": 58895,
                "step": "Outage Agreed w/ Business"
            },
            {
                "ID": 58896,
                "step": "GMLC/E911/CMAS Significant"
            }
        ]
        
        
        obj.map((ob)=>{
            const found=obj1.find((e)=>{
                return e.step===ob.value;
            });
            if(found){
                ob.ID=found.ID
            }
        });
        

        可能有错字,我只是输入了它,但你明白了。 您需要进行必要的检查以避免未定义。

        【讨论】:

          【解决方案6】:

          您可以为ID 取一个Map 并获取此值来映射新对象。

          var array1 = [{ ID: 58895, step: "Outage Agreed w/ Business" }, { ID: 58896, step: "GMLC/E911/CMAS Significant" }],
              array2 = [{ type: "verification_step", value: "GMLC/E911/CMAS Significant" }, { type: "verification_step", value: "Outage Agreed w/ Business" }],
              map = new Map(array1.map(({ ID, step }) => [step, ID])),
              result = array2.map(({ type, value: step }) => ({ ID: map.get(step), type, step }));
          
          console.log(result);

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-03-04
            • 2014-02-28
            • 2020-08-24
            • 2013-06-11
            • 1970-01-01
            相关资源
            最近更新 更多