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);
Related
This question already has answers here:
Filter array of objects based on another array in javascript
(10 answers)
Closed 3 months ago.
I am a newbie to JavaScript. I have now a nested Object:
const fruitList = [
{ fruit: {id: '1-1', fruit_name: 'Apple'},
location: {id: '2-1', location_name: 'USA'}
},
{
fruit: {id: '1-2', fruit_name: 'Banana'},
location: {id: '2-2', location_name: 'UK'}
},
{
fruit: {id: '1-3', fruit_name: 'Orange'},
location: {id: '2-1', location_name: 'USA'}
}
];
and a string array:
let keywords = ['Apple', 'Banana'];
I am trying to filter the nested Object based on the above string array and the expected outpust is :
output =[
{ fruit: {id: '1-1', fruit_name: 'Apple'},
location: {id: '2-1', location_name: 'USA'}
},
{
fruit: {id: '1-2', fruit_name: 'Banana'},
location: {id: '2-2', location_name: 'UK'}
}
];
I already tried:
const filteredFruit = fruitList.filter(({item})=>
item.fruit?.fruit_name.every(ele => keywords.includes(ele))
)
but it didn't work. I also checked all the similar questions on the Stackoverflow, but still could not find a way to solve it. Thank you very much for your help!
You were close.
item isn't a property of the object being iterated on so you can't destructure it.
You need to swap around your condition to check to see if the keywords array includes the fruit name.
const fruitList=[{fruit:{id:"1-1",fruit_name:"Apple"},location:{id:"2-1",location_name:"USA"}},{fruit:{id:"1-2",fruit_name:"Banana"},location:{id:"2-2",location_name:"UK"}},{fruit:{id:"1-3",fruit_name:"Orange"},location:{id:"2-1",location_name:"USA"}}];
const keywords = ['Apple', 'Banana'];
const filteredFruit = fruitList.filter(item =>
keywords.includes(item.fruit.fruit_name)
);
console.log(filteredFruit);
This question already has answers here:
How to get the difference between two arrays of objects in JavaScript
(22 answers)
Array.includes() to find object in array [duplicate]
(8 answers)
Closed 11 months ago.
I have an array of available items:
const items = [{id: 1, title: Item 1}, {id: 2, title: Item 2}, {id: 3, title: Item 3}]
and an array of items that my user has purchased:
const purchasedItems = [{id: 1, title: Item 1}, {id: 2, title: Item 2}]
I want to display an array of items that are still available to them to purchase that they haven't bought yet, i.e. just item 3 in this case. So I want an array returned with just Item 3 in it.
I have tried:
const availableItems = items.filter(
(item) => !purchasedItems.includes(item)
)
However this just returns a list of all the items again.
Think I must be missing something quite obvious, any help appreciated!
includes won't work here because it will only compare the items with a strict equality (as in item1 === item2). It will only works if your items are the same objects with the same reference.
A little example:
const obj1 = { test: 1 };
const obj2 = obj1;
const obj3 = { test: 1 };
console.log(obj1 === obj2); // true
console.log(obj1 === obj3); // false
So, in your case, you have to do a more complex work in your filter function:
const availableItems = items.filter(item1 => !purchasedItems.some(item2 => item1.id === item2.id));
Use findIndex() instead of includes
const items = [{ id: 1, title: 'Item 1' }, { id: 2, title: 'Item 2' }, { id: 3, title: 'Item 3' }]
const purchasedItems = [{ id: 1, title: 'Item 1' }, { id: 2, title: 'Item 2' }]
const availableItems = items.filter((item) => (purchasedItems.findIndex(purchasedItem => purchasedItem.id === item.id) == -1))
console.log(availableItems)
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 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)
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 4 years ago.
My API hands me this array of objects:
[
{ id: 5, name: "foo" },
{ id: 7, name: "bar" }
]
I would like to filter out the ID keys and achieve this:
[5,7]
What would be considered best practice in this case?
Just use array.map:
var data = [
{ id: 5, name: "foo" },
{ id: 7, name: "bar" }
];
var res = data.map(({id}) => id);
console.log(res);