2
function extend(obj1, obj2) {

  var obj2Keys = Object.keys(obj2);
  var obj2Values = Object.values(obj2);

  var obj1Keys = Object.keys(obj1);
  var obj1Values = Object.keys(obj1);
  
  var newObj = {};

  for(var i=0; i<obj1Keys.length; i++) {
    if(obj1Keys[i] !== obj2Keys[i]) {
      

    }
  }
  
}

var obj1 = {
  a: 1,
  b: 2
};
var obj2 = {
  b: 4,
  c: 3
};

extend(obj1, obj2);

console.log(obj1); // --> {a: 1, b: 2, c: 3}
console.log(obj2); // --> {b: 4, c: 3}

/*

 1. Add any keys that are not in the 1st object.
 2. If the 1st object already has a given key, ignore it  (do not
    overwrite the property value).
 3. Do not modify the 2nd object at all.

*/

Hey guys, trying to figure this one out. I'm sure i'm doing this the most inefficient way. I'm struggling to compare indexes of these objects.. Any help?? I was using for in loops originally but I couldn't conceptually understand what I was doing lol. Kinda stuck at this point.

2
  • Object.assign({}, second, first) - overwriting second's keys by the first's. Commented Mar 13, 2021 at 20:27
  • Is it a requirement that the original object is mutated? Commented Mar 13, 2021 at 20:30

3 Answers 3

3

const firstObject = {a:1,b:2,c:3}
const secondObject = {a:11,b:22,c:33,d:44}
var newObject = {...secondObject,...firstObject}

console.log(newObject)

result:

{"a":1,"b":2,"c":3,"d":44}
Sign up to request clarification or add additional context in comments.

6 Comments

This is identical to my answer
no, your answer is different. your output will be : {"a":11,"b":22,"c":33,"d":44}
@symlink "only if the first object doesn't have those keys".
Slightly different output, identical method.
but he is asking, not to replace the values of the first object
|
1
  1. Get keys of obj2
  2. Iterate over them using the for-loop
  3. In each iteration, if obj1 does not have this key, and the record to it from obj2

function extend (obj1, obj2) {
  const obj2Keys = Object.keys(obj2);
  for(let i = 0; i < obj2Keys.length; i++) {
    const currentKey = obj2Keys[i]
    if(!obj1[currentKey]) {
      obj1[currentKey] = obj2[currentKey];
    }
  }
}

const obj1 = { a: 1, b: 2 }; const obj2 = { b: 4, c: 3 };

extend(obj1, obj2);

console.log(obj1); // --> {a: 1, b: 2, c: 3}
console.log(obj2); // --> {b: 4, c: 3}

Another way using .forEach:

function extend (obj1, obj2) {
  Object.keys(obj2).forEach(currentKey => {
    if(!obj1[currentKey]) {
      obj1[currentKey] = obj2[currentKey];
    }
  });
}

const obj1 = { a: 1, b: 2 }; const obj2 = { b: 4, c: 3 };

extend(obj1, obj2);

console.log(obj1); // --> {a: 1, b: 2, c: 3}
console.log(obj2); // --> {b: 4, c: 3}

2 Comments

How is if(!obj1[currentKey]) different from if(obj1[currentKey] !== obj2[currentKey]) why do I get the right answer with the first statement and the wrong value with the second?
!obj1[currentKey] means that obj1 has no such key. obj1[currentKey] !== obj2[currentKey] means that for the key currentKey, thevalues from obj1 and obj2 are different. In your case, you want to only add a key-value pair from obj2 to obj1 if it doesn't exist in the first place.
1

Use the spread syntax to combine the objects. The spread syntax basically inserts the objects key/value pairs wherever you write it, so you're creating a new object with {...obj1, ...obj2}. If there are pairs with duplicate keys, the pair from the second object will be selected.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

let obj1 = {a: 1, b: 2}
let obj2 = {b: 4, c: 3}

let obj3 = {...obj2, ...obj1}
console.log(obj3)

3 Comments

I agree this is the most readable and simple way. However I think you should put ...obj1 last so it grabs its values on the shared keys
@FabioLopez yeah, it depends on which object's values he wants to take precedence. He didn't say so I used the second one, but explained how he could use either one by putting it last.
@symlink your answer is different. your script output will be : {"a":11,"b":22,"c":33,"d":44}

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.