简体   繁体   English

如果具有相同的键值,则将键值添加到对象数组

[英]Add key value to an object array if it has same key value

I'm trying to add a property with a certain value to all of the objects in one array, based on a corresponding value in another array.我正在尝试根据另一个数组中的相应值向一个数组中的所有对象添加一个具有特定值的属性。

const array1 = [
  {
    id: 1,
    date: '2022.05.01',
    name: 'john'
  }, {
    id: 2,
    date: '2022.05.01',
    name: 'sam'
  }, {
    id: 3,
    date: '2022.05.03',
    name: 'john'
  }, {
    id: 4,
    date: '2022.05.06',
    name: 'jack'
  },
 ]
    

This array contains the required modifications that need to be made:此数组包含需要进行的必要修改:

const array2 = [
  {
    name: 'john',
    isCanceled: true,
  }, {
    name: 'jack',
    isCanceled: false,
  }, {
    name: 'sam',
    isCanceled: false,
  },
 ]

If the name in the object within array1 is john then isCanceled should be set to true , but if it's jack or sam it should be set to false like so:如果array1中对象的名称是john ,则isCanceled应设置为true ,但如果是jacksam则应设置为false ,如下所示:

const resultArray = [
  {
    id: 1,
    date: '2022.05.01',
    name: 'john',
    isCanceled: true,
  }, {
    id: 2,
    date: '2022.05.01',
    name: 'sam'
    isCanceled: false,
  }, {
    id: 3,
    date: '2022.05.03',
    name: 'john'
    isCanceled: true,
  }, {
    id: 4,
    date: '2022.05.06',
    name: 'jack'
    isCanceled: false,
  },
 ];

Build a status lookup object containing the status, and map your array to include the matching values from the lookup object:构建一个包含状态的status查找对象,并映射您的数组以包含查找对象中的匹配值:

const status = Object.fromEntries(
  array2.map(({name, isCanceled}) => [name, isCanceled])
);

const resultArray = array1.map(({id, date, name}) => ({
  id,
  date,
  name,
  isCanceled: status[name]
}));

Alternatively, if you just want to modify array1 instead of creating a new array, the second step can be replaced with:或者,如果您只想修改array1而不是创建新数组,则可以将第二步替换为:

array1.forEach(v => v.isCanceled = status[v.name]);

Complete snippet:完整片段:

 const array1 = [{ id: 1, date: '2022.05.01', name: 'john' }, { id: 2, date: '2022.05.01', name: 'sam' }, { id: 3, date: '2022.05.03', name: 'john' }, { id: 4, date: '2022.05.06', name: 'jack' }, ]; const array2 = [{ name: 'john', isCanceled: true, }, { name: 'jack', isCanceled: false, }, { name: 'sam', isCanceled: false, }, ]; const status = Object.fromEntries( array2.map(({name, isCanceled}) => [name, isCanceled]) ); const resultArray = array1.map(({id, date, name}) => ({ id, date, name, isCanceled: status[name] })); console.log(resultArray);

Should you wonder why I prefer to build the lookup object instead of using find() inside the map() operation: in terms of complexity, the lookup table has a complexity of O(1) , while the find() approach is O(n) .您是否想知道为什么我更喜欢构建查找对象而不是在map()操作中使用find() :就复杂性而言,查找表的复杂度为O(1) ,而find()方法为O( n) .

you can do something as :你可以做一些事情:

const result = array1.map(person => {
  const lookPerson = array2.find(person2 => person2.name === person.name);
  return (lookPerson) ? { 
    ...person,
    isCanceled: lookPerson.isCanceled
  } : person;
});
  • combine array.map to transform array1结合 array.map 来变换 array1

  • use array.find to get first iteration of similar person between array1 and array2使用 array.find 在 array1 和 array2 之间获取相似人的第一次迭代

  • merge object with合并对象

    { ...person, isCanceled: lookPerson.isCanceled } 

 const array1 = [{ id: 1, date: '2022.05.01', name: 'john' }, { id: 2, date: '2022.05.01', name: 'sam' }, { id: 3, date: '2022.05.03', name: 'john' }, { id: 4, date: '2022.05.06', name: 'jack' }, ]; const array2 = [{ name: 'john', isCanceled: true, }, { name: 'jack', isCanceled: false, }, { name: 'sam', isCanceled: false, }, ]; const result = array1.map(person => { const lookPerson = array2.find(person2 => person2.name === person.name); return (lookPerson) ? { ...person, isCanceled: lookPerson.isCanceled } : person; }); console.log(result);

You can do this more simply by first converting your array2 into an object and then using a lookup within a map like so:您可以更简单地做到这一点,首先将您的array2转换为一个对象,然后在map中使用查找,如下所示:

 const array1 = [{id:1,date:"2022.05.01",name:"john"},{id:2,date:"2022.05.01",name:"sam"},{id:3,date:"2022.05.03",name:"john"},{id:4,date:"2022.05.06",name:"jack"},]; const array2 = [{name:"john",isCanceled:true},{name:"jack",isCanceled:false},{name:"sam",isCanceled:false}]; const arrCancel = array2.reduce((a, { name, isCanceled }) => { a[name] = isCanceled; return a; }, {}); const resultArr = array1.map(e => { e.isCanceled = arrCancel[e.name]; return e; }); console.log(resultArr);
 .as-console-wrapper { max-height: 100% !important; top: auto; }

You can do:你可以做:

 const array1 = [{id: 1,date: '2022.05.01',name: 'john'}, {id: 2,date: '2022.05.01',name: 'sam'}, {id: 3,date: '2022.05.03',name: 'john'}, {id: 4,date: '2022.05.06',name: 'jack'}] const array2 = [{name: 'john',isCanceled: true,}, {name: 'jack',isCanceled: false,}, {name: 'sam',isCanceled: false,}] const array2Hash = array2.reduce((a, { name: n, isCanceled: ic}) => (a[n] = ic, a), {}) const result = array1.map(u => ({ ...u, isCanceled: array2Hash[u.name] })) console.log(result)

resultArray is map of array1 with addition isCanceled based of array2 : resultArray 是array1的映射,加上基于array2isCanceled

resultArray = array1.map( a => Object.assign(
  {},
  a,
  {isCanceled: array2.find(b => a.name == b.name)?.isCanceled||false}) )

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM