Juan Juan - 1 year ago 78
Javascript Question

Access all properties inside an array of objects in Javascript

If i have an array of objects, how can i iterate through it?

For example this array

[{ name: "objc1", size: "small" }, { first: "objc2", size: "medium" }, { first: "objc3", size: "Big" }]

I can access each object with this:

for (var key in collection){



But how can i go inside the object, to each property?

Answer Source

Don' t use for ... in over arrays, this approach has several issues. The for ... in is actually made for iterating over properties. Therefore, use the following (ES6):

for (const item of collection) {
  for (const key in item) {
    alert(key+': '+item[key]);

On ES5, you need a loop like this:

var item;
for (var i=0; i < collection.length; i++) {
  item = collection[i];
  for (var key in item) {
    alert(key+': '+item[key]);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download