DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Discussion on: Master Objects in JS 🍨 (Part 1)

Collapse
 
mellen profile image
Matt Ellen • Edited on
  objectOneProperties.forEach((property, index) => {
    let propName = objectOneProperties[index];
    if (objectOne[propName] == objectTwo[propName]) {
      isEqual = true;
    } else {
      isEqual = false;
    }
  });
Enter fullscreen mode Exit fullscreen mode

This returns true if the last property values are equal, regardless of the other values.

It would make more sense to me if you use a regular for loop and break on false:

  let isEqual = true;

  for(let propName of objectOneProperties)
  {
    if(objectOne[propName] != objectTwo[propName])
    {
      isEqual = false;
      break;
    }
  }
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jrmatanda profile image
Ben Matt, Jr.

Thanks for the tip, but as you already know if it ain't broke don't touch it πŸ˜‰, but honestly I didn't though about it thanks πŸ‘

Collapse
 
mellen profile image
Matt Ellen

If you change personTwo to

let personTwo = {
  name: "james",
  lastName: "Owen",
};
Enter fullscreen mode Exit fullscreen mode

Then you still get true, but the properties are not all the same. Is that what you intended?

Thread Thread
 
jrmatanda profile image
Ben Matt, Jr.

No your feedbacks are priceless thanks a lot, I've changed it thanks