10

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);

is there a one-liner (i.e. not a custom function that iterates over the keys) that would ignore the property c in the source ? I would also like to avoid using libraries such as lodash or JQuery.

Duplicate EDIT my question isn't about merging two objects, my question is about ignoring the fields in the second object, that aren't in the first object.

  • 1
    @Mium not at all. I'm not trying to join them. I'm trying to ignore the new fileds, which is the exact opposite of the question you quoted. –  Jul 06 '18 at 09:22
  • IMHO, your solution is pretty short and readable, that's what I do too in such cases. Let's see, is there more brief way... – P.S. Jul 06 '18 at 09:24
  • @CommercialSuicide That's the shorter I've found, but I expected something signed like `Object.assign` ... –  Jul 06 '18 at 09:24
  • 1
    What about this `Object.keys(objectOne).reduce((r,k) => (r[k] = k in objectTwo ? objectTwo[k] : objectOne[k], r) , {});` ? – Hassan Imam Jul 06 '18 at 09:26
  • @Hassan this is basically the same thing as my current solution, which is a custom function, something I would like to avoid –  Jul 06 '18 at 09:27
  • Yeah, pretty much. – Hassan Imam Jul 06 '18 at 09:28
  • Have you tried like this `for(var i in objectOne) { current[i] = objectTwo[i] }` – Jack jdeoel Jul 06 '18 at 09:47
  • @DavidJawHpan this is longer than my current solution, and it's still a custom function –  Jul 06 '18 at 09:51
  • @RobC first of all that's not true, if you put `y: 10` in the first object and merge the second object in it, the `y` property will be kept. Second, if we assume it is indeed false, that's besides the question, since I asked to update only the existing fields with the keys of the second object. –  Jul 06 '18 at 10:46

3 Answers3

6

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)
0
const result = Object.keys(objectOne)
    .reduce((init, key) => Object.assign(init, {[key]: objectTwo[key]}) , {});
Reuven Chacha
  • 879
  • 8
  • 20
  • Again, this is a custom function, something I would like to avoid. What I would like is a built-in function such as `Object.assign`, but it ignores the new fields. –  Jul 06 '18 at 09:29
  • Didn't notice your edit, so the answer is irrelevant – Reuven Chacha Jul 06 '18 at 09:32
  • 1
    Still, keep it there, as it might help someone looking for it ! –  Jul 06 '18 at 09:32
  • This fails if `objectOne` has properties that `objectTwo` doesn't, making those properties `undefined`. – OfirD Jan 13 '21 at 21:56
0

I think there is no built-in function to accomplish what you need, rather than a custom one with iterating over keys of the first object and values of the second one:

const objectOne = {
  a: 0,
  b: 0
};

const objectTwo = {
  a: 1,
  b: 1,
  c: 1,
};

const result = Object.keys(objectOne).reduce((all, key) => {

    all[key] = objectTwo[key] || objectOne[key];

    return all;

},{});

console.log(result);
Leonid Pyrlia
  • 1,594
  • 2
  • 11
  • 14
  • 2
    Okay, so the best I can do is stay with my current solution ... thank you for the tip –  Jul 06 '18 at 09:51