通过键从对象合并到对象数组

时间:2018-10-08 19:13:36

标签: javascript arrays object

我的目标是通过将id与另一个包含匹配键的对象进行匹配来更新对象数组,以更新对象数组的value键。

使用对象数组,如:

const objArr = [
  {
    id: 6,
    name: 'Activity Attendance',
    type: 'Number',
    value: '2000',
    nameChanged: false,
    typeChanged: false,
    internalName: 'activity_attendance'
  },
  {
    id: 123,
    name: 'Total Number of Interacted Consumers',
    type: 'Number',
    value: '400',
    nameChanged: false,
    typeChanged: false,
    internalName: 'total_number_of_interacted_consumers'
  },
  {
    id: 140,
    name: 'Booth Location',
    type: 'Select (Single Answer)',
    value: '',
    nameChanged: false,
    typeChanged: false,
    internalName: 'booth_location'
  }
];

还有一个像这样的对象

const obj = {
  '6': '1500',
  '123': '180',
  '140': ''
};

所需结果:

const objArr = [
  {
    id: 6,
    name: 'Activity Attendance',
    type: 'Number',
    value: '1500',
    nameChanged: false,
    typeChanged: false,
    internalName: 'activity_attendance'
  },
  {
    id: 123,
    name: 'Total Number of Interacted Consumers',
    type: 'Number',
    value: '180',
    nameChanged: false,
    typeChanged: false,
    internalName: 'total_number_of_interacted_consumers'
  },
  {
    id: 140,
    name: 'Booth Location',
    type: 'Select (Single Answer)',
    value: '',
    nameChanged: false,
    typeChanged: false,
    internalName: 'booth_location'
  }
];

我已经开始了映射objArr的路线,但是不确定如何在obj内进行“匹配”以获取正确的值进行更新。我失败的尝试:

objArr.map(i => i.id.includes(Object.keys(obj).map(o => Number(o))));

如何从一个对象合并成一个对象数组?

5 个答案:

答案 0 :(得分:1)

以下是使用map()函数并扩展(...)运算符的解决方案:

var objArr = [
  {
    id: 6,
    name: 'Activity Attendance',
    type: 'Number',
    value: '2000',
    nameChanged: false,
    typeChanged: false,
    internalName: 'activity_attendance'
  },
  {
    id: 123,
    name: 'Total Number of Interacted Consumers',
    type: 'Number',
    value: '400',
    nameChanged: false,
    typeChanged: false,
    internalName: 'total_number_of_interacted_consumers'
  },
  {
    id: 140,
    name: 'Booth Location',
    type: 'Select (Single Answer)',
    value: '',
    nameChanged: false,
    typeChanged: false,
    internalName: 'booth_location'
  }
];

var obj = {
  '6': '1500',
  '123': '180',
  '140': ''
};

console.log(objArr.map(i => ({
  ...i,
  value: obj[i.id] ? obj[i.id] : i.value 
})))

答案 1 :(得分:0)

如果您可以修改原始数组,可以解决问题:

只需遍历原始对象并检查是否有更新

objArr.forEach(o=>{
    if( obj[o.id] )
        o.value = obj[o.id]
});

示例:

var objArr = [
  {
    id: 6,
    name: 'Activity Attendance',
    type: 'Number',
    value: '2000',
    nameChanged: false,
    typeChanged: false,
    internalName: 'activity_attendance'
  },
  {
    id: 123,
    name: 'Total Number of Interacted Consumers',
    type: 'Number',
    value: '400',
    nameChanged: false,
    typeChanged: false,
    internalName: 'total_number_of_interacted_consumers'
  },
  {
    id: 140,
    name: 'Booth Location',
    type: 'Select (Single Answer)',
    value: '',
    nameChanged: false,
    typeChanged: false,
    internalName: 'booth_location'
  }
];

var obj = {
  '6': '1500',
  '123': '180',
  '140': ''
};

// loop through the array of objects
objArr.forEach(o=>{

    // see if there is an update in the `obj` var
    if( obj[o.id] )
        o.value = obj[o.id] // apply the update
});

console.log(objArr)

答案 2 :(得分:0)

在普通的javascript中,我会做类似的事情

ModelBinder

答案 3 :(得分:0)

您可以这样做(不会修改objArr的原始值):

const result = objArr.map(o => Object.keys(obj).includes(o.id.toString()) ? Object.assign(o, {value: obj[o.id]}) : o);
  • Object.keys(obj).includes(o.id.toString()):检查obj中的条目是否具有与id中的objArr对应的键(请参阅Object.keys
  • Object.assign(o, {value: obj[o.id]}) : o):如果是这样,请使用objArr中的值创建一个新对象,但要覆盖value(请参见Object.assign)。如果不是,则返回相同的对象。

演示:

const objArr = [
  {
    id: 6,
    // ...
    value: '2000'
    // ...
  },
  {
    id: 123,
    // ...
    value: '400'
    // ...
  },
  {
    id: 140,
    // ...
    value: ''
    // ...
  }
];

const obj = {
  '6': '1500',
  '123': '180',
  '140': ''
};

const result = objArr.map(o => Object.keys(obj).includes(o.id.toString()) ? Object.assign(o, {value: obj[o.id]}) : o);

console.log(result);

答案 4 :(得分:0)

您可以使用以下示例解决问题:

objArr.forEach(i => i.value = Object.keys(obj).includes(String(i.id)) ? obj[i.id]: i.value);