基于不同的键但值相同的对象合并2个数组

时间:2019-06-04 16:06:45

标签: javascript arrays object array-merge

我已提及以下问题,但没有相关答案。

  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-

public static T? GetNullableValueType<T>(this SqlDataReader sqlDataReader, string columnName) where T : struct
{
    int columnOrdinal = sqlDataReader.GetOrdinal(columnName);
    return sqlDataReader.IsDBNull(columnOrdinal) ? (T?)null : sqlDataReader.GetFieldValue<T>(columnOrdinal);
}

public static T GetNullableReferenceType<T>(this SqlDataReader sqlDataReader, string columnName) where T : class
{
    int columnOrdinal = sqlDataReader.GetOrdinal(columnName);
    return sqlDataReader.IsDBNull(columnOrdinal) ? null : sqlDataReader.GetFieldValue<T>(columnOrdinal);
}

public static T GetNonNullValue<T>(this SqlDataReader sqlDataReader, string columnName)
{
    int columnOrdinal = sqlDataReader.GetOrdinal(columnName);
    return sqlDataReader.GetFieldValue<T>(columnOrdinal);
}

OBJ2-

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

我想根据字符串的值得到包含单个对象中两个值的输出,即

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

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

修改

作为参考的第三参考不是这种情况。密钥应保留为“步骤”,并且数据应合并。

6 个答案:

答案 0 :(得分:2)

您可以在合并两个数组后使用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))

答案 1 :(得分: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)

答案 2 :(得分:1)

您可以使用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()

答案 3 :(得分:0)

由于需要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
    }
});

可能有一个错字,我只是打了一下,但您明白了。 您需要进行必要的检查以避免未定义。

答案 4 :(得分:0)

你可以写 这是测试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 :(得分:0)

您可以将Map用作ID,并获取此值以映射新对象。

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);