0

I have two arrays in Javascript, I need to update the first array with the contents of the second array using the key of id.

I need to find the id from the array and update the same object.

since b is an array of a single object, I can use b[0].id

const a = [{id: 1, name: 'test', data: 'yes'}, {id: 2, name: 'test 1'}]

const b = [{id: 1, name: 'sample', data: 'no'}, {id: 3, name: 'sample', data: 'no'}]

const output = [{id: 1, name: 'sample', data: 'no'}, {id: 2, name: 'test 1'}, {id: 3, name: 'sample', data: 'no'}]

Any help will be appreciated

1
  • Do you want to update the object in place, or replace the entire object? Commented Apr 8, 2019 at 7:09

3 Answers 3

3

You could take a Map and map new objects with the properties of a and b.

const
    a = [{ id: 1, name: 'test', data: 'yes' }, { id: 2, name: 'test 1' }],
    b = [{ id: 1, name: 'sample', data: 'no' }],
    map = new Map(b.map(o => [o.id, o])),
    result = a.map(o => Object.assign({}, o, map.get(o.id)));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

For getting a new array with all items, you could take a closure over the result array and a Map.

const
    merge = (target, map = new Map) => source => {
        var object = {};
        if (!map.has(source.id)) {
            map.set(source.id, object);
            target.push(object);
        }
        Object.assign(map.get(source.id), source);
    },
    a = [{ id: 1, name: 'test', data: 'yes' }, { id: 2, name: 'test 1' }],
    b = [{ id: 1, name: 'sample', data: 'no' }, { id: 3, name: 'sample', data: 'no' }],
    result = [],
    update = merge(result);

[a, b].forEach(a => a.forEach(update));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

2 Comments

if the second array b is having a new id like id: 3, how can i add that one to the result
i have updated the question i have another object id as 3 in the b array how can i merge that one to the result
2

Use map and some with spreading:

const a = [{id: 1, name: 'test', data: 'yes'}, {id: 2, name: 'test 1'}];
const b = [{id: 1, name: 'sample', data: 'no'}];

const output = a.map(e => b.some(({ id }) => id == e.id) ? ({ ...e, ...b.find(({ id }) => id == e.id)}) : e);

console.log(output);

1 Comment

i have updated the question i have another object id as 3 in the b array how can i merge that one to the result
2

I would do something like this:

const a = [{id: 1, name: 'test', data: 'yes'}, {id: 2, name: 'test 1'}];

const b = [{id: 1, name: 'sample', data: 'no'}];

let output = [];

output = a.map((element) => {
    const newEl = b.filter(e => e.id === element.id);
    if (newEl[0] != undefined) {
        return newEl[0];
    } else {
        return element;
    }
});

console.log(output);

UPDATE:

As you updated your question, this could be a possible solution:

const a = [{id: 1, name: 'test', data: 'yes'}, {id: 2, name: 'test 1'}]

const b = [{id: 1, name: 'sample', data: 'no'}, {id: 3, name: 'sample', data: 'no'}]

let output = [];

Array.prototype.unique = function() {
    let a = this.concat();
    for(let i=0; i<a.length; ++i) {
        for(let j=i+1; j<a.length; ++j) {
            if(a[i].id === a[j].id)
                a.splice(j--, 1);
        }
    }
    return a;
};

const mergedArrays = a.concat(b).unique();

output = mergedArrays.map((element) => {
    const newEl = b.filter(e => e.id === element.id);
    if (newEl[0] != undefined) {
        return newEl[0];
    } else {
        return element;
    }
});

console.log(output);

Basically you merge the two starting arrays a and b, using a custom Arrays function called unique that checks for duplicates and remove them a[i].id === a[j].id, based on IDs. Then you proceed as usual doing a map on the new mergedArrays.

1 Comment

i have updated the question i have another object id as 3 in the b array how can i merge that one to the result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.