简体   繁体   中英

JS. Updating objects in an array (matching by value)

Have first array (a) and second with updated values (b). Need to update matching by IDs objects and get result array (c). How can I make it simple and fast?

let a = [
    { id: 1, activated: '0' },
    { id: 2, activated: '0' },
    { id: 3, activated: '0' },
    { id: 4, activated: '0' },
  ]
let b = [
    { id: 2, activated: '1' },
    { id: 3, activated: '1' },
  ]
//Result array:
c = [ 
  { id: 1, activated: '0' },
  { id: 2, activated: '1' },
  { id: 3, activated: '1' },
  { id: 4, activated: '0' },
]

I recommend you to create an object where keys are the ids for fast access.

With the newly created object you can access the specific objects from b using the id as key and update the objects.

Finally, get the array c using the function Object.values .

This is assuming that b has ids that exist in a .

 let a = [ { id: 1, activated: '0' }, { id: 2, activated: '0' }, { id: 3, activated: '0' }, { id: 4, activated: '0' }] let b = [ { id: 2, activated: '1' }, { id: 3, activated: '1' }]; let newA = a.reduce((a, {id, ...rest}) => ({...a, ...{[id]: {id, ...rest}}}), {}); b.forEach(({id, activated}) => newA[id].activated = activated); let c = Object.values(newA); console.log(c);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

You can use Array.map , then foreach object, you can use Array.find to check if it exist in arrayB by ID and return if it does.

let a = [
    { id: 1, activated: '0' },
    { id: 2, activated: '0' },
    { id: 3, activated: '0' },
    { id: 4, activated: '0' },
];

let b = [
    { id: 2, activated: '1' },
    { id: 3, activated: '1' },
];

const c = a.map(e => {
    let newValue = b.find(n => e.id === n.id);

    if (!!newValue) {
        return newValue;
    }

    return e;
});

This is how I would approach it:

 function mergeArray(...toMerge) { let output = {}; toMerge.forEach(arr => { arr.forEach(item => { output[item.id] = item; }); }); return Object.values(output); } let a = [ { id: 1, activated: '0' }, { id: 2, activated: '0' }, { id: 3, activated: '0' }, { id: 4, activated: '0' }, ]; let b = [ { id: 2, activated: '1' }, { id: 3, activated: '1' }, ]; console.dir(mergeArray(a, b)) // can merge n-number of arrays.

let c = a.map( aObj => {
  let found = b.find(bObj => bObj.id === aObj.id);
    if(found) return found;
    else return aObj;
});

You could create a map of the second array, keyed by id , and then when you make a copy of a objects into c , use that map to merge matching b objects into them.

 let a = [{ id: 1, activated: '0' },{ id: 2, activated: '0' },{ id: 3, activated: '0' },{ id: 4, activated: '0' }] let b = [{ id: 2, activated: '1' },{ id: 3, activated: '1' }]; let map = new Map(b.map(o => [o.id, o])); let c = a.map(o => ({...o, ...map.get(o.id)})); console.log(c);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

Building a hash from the ids of array a. Iterate over array b by looking in the hash for the index b.id, if found actualisize in a with the founded index the property activated. Otherwise (in example id: 5) there don't exists in the old array a for this id an entry so create a new one and actualisize the hash.

 let a = [ { id: 1, activated: '0' }, { id: 2, activated: '0' }, { id: 3, activated: '0' }, { id: 4, activated: '0' }, ]; let b = [ { id: 2, activated: '1' }, { id: 3, activated: '1' }, { id: 5, activated: '0' }, ]; let hash = a.flatMap(el => el.id); b.forEach(elem => { index = hash.indexOf(elem.id); if (index.=-1) a[index].activated=elem;activated. else { a;push(elem). hash.push(elem;id); } }). console;log(a);

Try this

 let a = [{ id: 1, activated: '0' }, { id: 2, activated: '0' }, { id: 3, activated: '0' }, { id: 4, activated: '0' }, ] let b = [{ id: 2, activated: '1' }, { id: 3, activated: '1' }, ] //Result array: c = [{ id: 1, activated: '0' }, { id: 2, activated: '1' }, { id: 3, activated: '1' }, { id: 4, activated: '0' }, ] let result = a.map(e => { let p = b.find(be => be.id === e.id) if (p) e.activated = p.activated; return e }) console.log(result)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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