Javascript retrieving data from an array [duplicate] - javascript

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)

Related

how do i find and return a specific object key and return as array in lodash [duplicate]

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)

How to access an object using javascript? [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 2 years ago.
i have the data like below when i log it in browser console.
const items = getItems(); //this has the logged value
{data: {..}}
data:
outer:
first: []
second: Arrary[2]
0: {id: '1', name: 'name1'}
1: {id: '2', name: 'name2'}
I want the second array of objects so the output should be
second: Array[2]
0: {id: '1', name: 'name1'}
1: {id: '2', name: 'name2'}
i have tried below,
const output = data.outer.second
but says cannot find data.
could someone help me fix this. thanks.
What object you use inside console.log()?. Suppose you are using object items as console.log(items) then you need to get second array like below.
let second = items.data.outer.second;
You can test it below.
const items = {
data: {
outer: {
first: [],
second: [{
id: '1',
name: 'name1'
}, {
id: '2',
name: 'name2'
}]
}
}
}
let second = items.data.outer.second;
console.log(second);

Remove duplicates from array of objects - Javascript [duplicate]

This question already has answers here:
How to remove all duplicates from an array of objects?
(77 answers)
Closed 2 years ago.
Hi I'm trying to remove duplicates from array of objects. But that is not working as expected.
This is my array:
const arr = [{
PData: [{
id: '1',
name: 'Book'
},
{
id: '2',
name: 'Bag'
},
{
id: '2',
name: 'Bag'
},
]
}]
const RemoveDuplicates = (array, key) => {
return array.reduce((arr, item) => {
const removed = arr.filter(i => i[key] !== item[key]);
return [...removed, item];
}, []);
};
var result = RemoveDuplicates(arr, 'id')
console.log(result);
Expected output:
[{
PData: [{
id: '1',
name: 'Book'
},
{
id: '2',
name: 'Bag'
},
]
}]
Based on id it supposed to remove duplicates but this is not happening currently..I know that couple of questions are existed regarding this but nothing is working for me. So anyone plz suggest me how to do.
You can use filter here is how on id and name.
const arr = [{
PData: [{
id: '1',
name: 'Book'
},
{
id: '2',
name: 'Bag'
},
{
id: '2',
name: 'Bag'
},
]
}]
arr[0].PData = Object.values(arr[0].PData).filter((v,i,a)=>a.findIndex(t=>(t.id === v.id && t.name=== v.name))===i)
console.log(arr[0].PData);

How to map an array of objects by a given field? [duplicate]

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)

ES6 map an array of objects, to return an array of objects with new keys [duplicate]

This question already has answers here:
ECMAScript 6 arrow function that returns an object
(6 answers)
Closed 6 years ago.
I have an array of objects:
[
{
id: 1,
name: 'bill'
},
{
id: 2,
name: 'ted'
}
]
Looking for a simple one-liner to return:
[
{
value: 1,
text: 'bill'
},
{
value: 2,
text: 'ted'
}
]
So I can easily pump them into a react dropdown with the proper keys.
I feel like this simple solution should work, but I'm getting invalid syntax errors:
this.props.people.map(person => { value: person.id, text: person.name })
You just need to wrap object in ()
var arr = [{
id: 1,
name: 'bill'
}, {
id: 2,
name: 'ted'
}]
var result = arr.map(person => ({ value: person.id, text: person.name }));
console.log(result)

Categories

Resources