Lend Sham
Lend Sham

Reputation: 63

Looping over array of object in javascript

How to loop to get all of the address ?

const name = {
  john: [
    {
      age: 21,
      address: 'LA',
    }
  ],
  sam: [
    {
      age: 26,
      address: 'California'
    }
  ]
}

I have code like this, and still get stuck how the flow is going

const array = Object.entries(name);
for (let i = 0; i < array.length; i++) {
  console.log(array[i]);
}

Upvotes: 1

Views: 74

Answers (3)

Sparrow
Sparrow

Reputation: 390

UPDATED ANSWER

If ObjectValue have multiple arrays. Please check below code and also I have written some comments in between code.

const name = { john: [ { age: 21, address: 'LA', }, { age: 23, address: 'Franch', } ], sam: [ { age: 26, address: 'California' }, { age: 24, address: 'Swiss' } ] }

var ObjectValues = Object.values(name);


// if single user have multiple data or address apply map method to ObjectValue too

var result = ObjectValues.map((ObjectValue) => {
    return ObjectValue.map(item => item.address);
});

// try to print result before combining
// console.log(result);

// combine all child arrays into single array
result = [].concat.apply([], result);

console.log(result);

Using forEach loop and get all address in single array

const name = { john: [ { age: 21, address: 'LA', }, { age: 23, address: 'Franch', } ], sam: [ { age: 26, address: 'California' }, { age: 24, address: 'Swiss' } ] }

var ObjectValues = Object.values(name);
var result = [];

ObjectValues.forEach((ObjectValue) => {
  ObjectValue.map(item => result.push(item.address));
});

console.log(result);

Simply write a function for best practice

const name = { john: [ { age: 21, address: 'LA', }, { age: 23, address: 'Franch', } ], sam: [ { age: 26, address: 'California' }, { age: 24, address: 'Swiss' } ] }

console.log(getAddress(name));


function getAddress(data) {
  let result = []; // initialize storage
  
  Object.values(data).forEach(ObjectValue => {
    // store address(data)
      ObjectValue.map(item => result.push(item.address));
  });
  return result; // return data
}

OLD ANSWER

Object.entries will return arrays of Object in [key, value] pair

So instead of using Object.entries use Object.values(it will return only "value" list of an object)

Now after extracting all value list with Object.values, now simply use map or forEach method to to get All address list

const name = {
  john: [
    {
      age: 21,
      address: 'LA',
    }
  ],
  sam: [
    {
      age: 26,
      address: 'California'
    }
  ]
}

var ObjectValues = Object.values(name);

// map method
var result = ObjectValues.map(ObjectValue => ObjectValue[0].address);
// here I have used 0 index of an ObjectValue because ObjectValue is an array of single Object of data {age, address}

console.log(result) // check result




// forEach method
var result = []

ObjectValues.forEach(ObjectValue => result.push(ObjectValue[0].address));

console.log('With forEach method (❁´◡`❁)')
console.log(result)

Upvotes: 1

Dev-2019
Dev-2019

Reputation: 557

This should do the trick,

Object.values(name).map(([{address}])=>address) // ["LA", "California"]

Upvotes: 0

Alvin Teh
Alvin Teh

Reputation: 787

In your case, name is acting more like a hash map as the keys are strings (e.g. john and sam) as opposed to numbers (0, 1, 2, etc.). Object.entries() returns key-value pairs (see MDN), and this is why the array[i] does not work.

Changing the loop slightly like below should fix things:

const array = Object.entries(name);

for (const [key, value] of Object.entries(array)) {
  console.log(`${key}: ${value}`);
  // logs john: [object Object] and sam: [object Object]
}

Upvotes: 0

Related Questions