jerry = {
weight: 178
}
Malcom = {
weight: 220
}
Bob = {
Weight: 134
}
people = [jerry, Malcom, Bob]
console.log(people[0]);
I'm trying to get a console.log of the object's name, "jerry". thanks for any and all help!
jerry = {
weight: 178
}
Malcom = {
weight: 220
}
Bob = {
Weight: 134
}
people = [jerry, Malcom, Bob]
console.log(people[0]);
I'm trying to get a console.log of the object's name, "jerry". thanks for any and all help!
ES6 Version: Using Object#entries, Array#forEach, and destructuring
const jerry = {weight: 178}
const Malcom = {weight: 178}
const Bob = {weight: 178}
const people = {jerry, Malcom, Bob}
const res = Object.entries(people).forEach(([name, {weight}])=>{
console.log(name, weight);
});
You can't. Jerry,Malcom, and Bob are just the variable names, you have two obvious solutions:
Add a name
attribute to your objects.
var jerry = {
name: "jerry",
weight: 178
}
Or change your array to an object, and use the key as the name of your object.
var people = {jerry: jerry, malcom: Malcom, bob: Bob}
For example:
var jerry = {
weight: 178
}
var Malcom = {
weight: 178
}
var Bob = {
weight: 178
}
var people = {jerry: jerry, malcom: Malcom, bob: Bob}
for(var person in people){
if(people.hasOwnProperty(person)){
console.log(person, people[person].weight);
}
}
var Some = {
MyValue : 1234
}
for (var i in Some) console.log(i);
var people = {
jerry : { weight: 178 },
Malcom : { weight: 220 },
Bob : { weight: 134 }
}
for (var i in people) console.log( i +' : '+ people[i].weight )