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)