'How to get only some of the elements of an object using only for-in and if-else
For example, if we have the following object in javascript:
var person,
property;
person = {
firstName: "John",
lastName:"Doe",
age: 50,
eyeColor: "blue",
hairColor: "black",
profession: "doctor"
}
How do I print out only the second and fourth property of this object with for-in and if-else?
Solution 1:[1]
another way would be.
var person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue",
hairColor: "black",
profession: "doctor"
}
for(key in person){
var index = Object.keys(person).indexOf(key);
if(index === 1 || index === 3){
console.log("Key:", key, ", Value:", person[key]);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nelsongt77 |
