Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

add in object by for in in javascript

I am new in javascript
I have object. When I try to add the city, it doesn’t appear in the message.
This object show us the detail person. but when I want to put city (forExample)
it doesn’t show in message

let person = {

  name: 'Sam',
  age: 30,
  employed: true,
  is_maried: false

}

and

for (let key in person){


  let massage = `key: ${key} and the value is ${person[key]}`;
  //this to give you the key name and the value in on line

  if (key in person === 'city'){

    person.city = 'NY';


  }else if (!(key in person !== 'city')){

      person.city = 'UK';

  }

console.log(massage);
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

let person = {
  name: 'Sam',
  age: 30,
  employed: true,
  is_maried: false
}

if (person.city) {
  person.city = "NY";
} else {
  person.city = "UK";
}

console.log(person);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading