This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 3 years ago.
I have an array of objects:
let tempArray = [
{
id: '1',
name: 'Tom',
age: 11
},
{
id: '2',
name: 'Jerry',
age: 13
}
...
]
How can I create a new array that would contain only name fields from all objects of the tempArray array?
Trying using map()
let tempArray = [
{
id: '1',
name: 'Tom',
age: 11
},
{
id: '2',
name: 'Jerry',
age: 13
}
]
const res = tempArray.map(i => i.name)
console.log(res)
Related
This question already has answers here:
Extract certain properties from all objects in array
(5 answers)
Remove property for all objects in array
(18 answers)
Closed 9 months ago.
I have following part of code: I would like to create new Array of objects based on this arr but only with age and id property
const arr = [
{
name: "A",
age: 1,
id: 11,
},
{
name: "B",
age: 2,
id: 22,
},
{
name: "C",
age: 3,
id: 33,
},
{
name: "D",
age: 4,
id: 44,
},
];
Thanks for your question Try this:
var newArr=arr.map(item=>{
return {
id:item.id,
age :item.age
}
})
This question already has answers here:
How to get all properties values of a JavaScript Object (without knowing the keys)?
(25 answers)
Closed 10 months ago.
Input data
const users = [{id: 1, name: 'Madou', age: 37},
{id: 2, name: 'Fatoumata', age: 33},
{id: 3, name: 'Amadou', age: 31}];
Output
[ [ 1, 'Madou', 37 ], [ 2, 'Fatoumata', 33 ], [ 3, 'Amadou', 31 ] ]
I implemented this:
const _data = users.map((item) => {
return Object.keys(item).map((value) => item[value]);
});
console.log(_data);
But I want to use REDUCE instead, but I don't have any how to do it
You can use Array.map() as follows:
const users = [{id: 1, name: 'Madou', age: 37},
{id: 2, name: 'Fatoumata', age: 33},
{id: 3, name: 'Amadou', age: 31}];
const result = users.map(o => Object.values(o));
console.log(result);
const _data = users.reduce((acc, { id, name, age }) => {
acc.push([id, name, age]);
return acc;
}, []);
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 1 year ago.
I'm failing to achieve something very simple. I can't retrieve specific data of an array. I've browsed through the documentation and similar threads on stack overflow but none seem to work.
For example:
[
{
id: 123,
name: 'Name1',
},
{
id: 456,
name: 'Name2',
},
{
id: 789,
name: 'Name3',
},
]
How could I possibly get all 'name' variables from each {} ?
You can use forEach or map to get a specific property from the list of objects:
let data = [{
id: 123,
name: 'Name1',
},
{
id: 456,
name: 'Name2',
},
{
id: 789,
name: 'Name3',
},
]
let names = []
data.forEach(element => names.push(element.name))
console.log(names)
names = data.map(element => element.name)
console.log(names)
You simply need to use .map (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)
The following code should do the job.
const names = yourArray.map(e=> e.name)
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 2 years ago.
how do I find and return a specific object key and return as array in lodash.
const category = [
{ id: 123, name: "haha"},
{ id: 124, name: "haha2"},
{ id: 125, name: "haha3"},
]
how do i get this?
result: [123,124,125]
make for each loop to iterate over category and each element of category has id push it into empty arr
let result = []
const category = [
{ id: 123, name: "haha"},
{ id: 124, name: "haha2"},
{ id: 125, name: "haha3"},
]
category.forEach(c=>{
result.push(c.id)
})
console.log(result)
You can use Array.map:
category.map(item => item.id)
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 6 years ago.
I have an array of objects like this:
[
{
id: 1,
name: 'Bill',
age: 42
},
{
id: 2,
name: 'Jack',
age: 37
}
]
I know how to copy all of it to the new array by using array spreads ... in es6 instead of slice()
const newArray = [...oldArray];
I just want to extract the name using the method below:
const newArray = [];
for (i = 0; i < oldArray.length; i++) {
newArray.push(oldArray[i].name);
}
Is there a better method to do this in es6?
Use Array.prototype.map():
let people =
[
{
id: 1,
name: 'Bill',
age: 42
},
{
id: 2,
name: 'Jack',
age: 37
}
]
let names = people.map(person => person.name)
jsbin.