The Index Of A Js Object Returns All Values In Object Rather Than The Actual Index
Let me explain this really quickly, when I fetch data from the database, there's a parent object and inside there is an array of objects, what I'm looking to achieve is to write th
Solution 1:
Can you try to apply the below concept in your example?
Consider you have a JSON structure like
varobj= {
a: {
blue:16,
green:30,
red:39
},
b: {
orange:56,
yellow:34,
white:35
},
c: {
black:3,
brown:43,
purple:12
}
};
Now, when you run a for...in
loop over this object
for (let key in obj) {
list.push(obj[key]);
keys.push(a);
}
obj[key]
will return the value {blue:16,green:30,red:39}
which ultimately is pushed into the list array
If you want the values blue, green and red, you can do something like
Object.keys(obj[key]); //will return an array ['blue', 'green', 'red']
Post a Comment for "The Index Of A Js Object Returns All Values In Object Rather Than The Actual Index"