1

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]);
}
Lend Sham
  • 53
  • 1
  • 8

3 Answers3

1

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)
Sparrow
  • 280
  • 1
  • 12
  • Thank you sir. I have a question, what if in the ObjectValue is not a single Object but multiple object with the same key, how to loop to get the all of address ? for example: ``` const name = { john: [ { age: 21, address: 'LA', }, { age: 23, address: 'Franch', } ], sam: [ { age: 26, address: 'California' }, { age: 24, address: 'Swiss' } ] } ``` – Lend Sham Oct 09 '21 at 15:12
  • Yes, you can do easily. Can I update my answer? – Sparrow Oct 09 '21 at 15:18
  • yes of course, you can update your answer – Lend Sham Oct 09 '21 at 15:20
  • @LendSham I have updated my answer and written three different methods. and Also read comments in code. – Sparrow Oct 09 '21 at 15:53
0

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]
}
Alvin Teh
  • 778
  • 1
  • 10
  • 17
0

This should do the trick,

Object.values(name).map(([{address}])=>address) // ["LA", "California"]
Dev-2019
  • 547
  • 3
  • 11