I'm using Typescript and I would like to update an object with another, only on the matching keys.
// Destination objectOne = { a: 0, b: 0, }; // Source objectTwo = { a: 1, b: 1, c: 1, }; // Expected result = { a: 1, b: 1, }; // Current solution const current = {}; Object.keys(objectTwo).forEach(key => key in objectOne ? current[key] = objectTwo[key] : null); console.log(current);
After a while without answers, it seems the shortest solution is the one I provided myself :
Object.keys(newObj).forEach(key => key in oldObj? result[key] = newObj[key] : null)
1.4m articles
1.4m replys
5 comments
57.0k users