Reputation: 79
I am using node.js and express and need to filter out the result of the array of json object
This is my array of objects
[
{
id: 1,
name: "Person 1",
boughtItems: {
item: "Shoes",
currency: "GBP",
cost: 200
},
{
item: "Bag",
currency: "EUR",
cost: 300
}
},
{
id: 2,
name: "Person 2",
boughtItems: {
item: "Shirt",
currency: "GBP",
cost: 13
},
{
item: "Jacket",
currency: "CAD",
cost: 150
}
}
]
by use of an endpoint ex: /continents?currency=EUR
I want to filter the results like this
[
{
id: 1,
name: "Person 1",
boughtItems:
{
item: "Bag",
currency: "EUR",
cost: 300
}
}
]
and for ex: /continents?currency=GBP
[
{
id: 1,
name: "Person 1",
boughtItems: {
item: "Shoes",
currency: "GBP",
cost: 200
}
},
{
id: 2,
name: "Person 2",
boughtItems: {
item: "Shirt",
currency: "GBP",
cost: 13
}
}
]
should I be using filter method to do this?
Upvotes: 1
Views: 1773
Reputation: 2966
just use filter
const array = [
{
id: 1,
name: "Person 1",
boughtItems: [{
item: "Shoes",
currency: "GBP",
cost: 200
},
{
item: "Bag",
currency: "EUR",
cost: 300
}]
},
{
id: 2,
name: "Person 2",
boughtItems: [{
item: "Shirt",
currency: "GBP",
cost: 13
},
{
item: "Jacket",
currency: "CAD",
cost: 150
}]
}
];
const filterData = (array, filterValue) => array.filter(obj => (obj.boughtItems = obj.boughtItems.filter(o => o.currency === filterValue)).length);
console.log(filterData(array, 'GBP'));
Upvotes: 0
Reputation: 17616
Your Javascript Array was not valid. boughtItems
should be a list, I modified the Array, if that was truly what you meant.
This does not mutate the original data
Use Array#reduce and Array#filter
const data = [{
id: 1,
name: "Person 1",
boughtItems: [{
item: "Shoes",
currency: "GBP",
cost: 200
},
{
item: "Bag",
currency: "EUR",
cost: 300
}
]
},
{
id: 2,
name: "Person 2",
boughtItems: [{
item: "Shirt",
currency: "GBP",
cost: 13
},
{
item: "Jacket",
currency: "CAD",
cost: 150
}
]
}
]
//get currency via request params
const c = "EUR";
const res = data.reduce((acc, {boughtItems, id, name}) => {
//filter through all items, and get those that match currency
const items = boughtItems.filter(({currency}) => currency === c);
//if there were some that matched, create the object with the items that match
if (items.length > 0) {
acc.push({id,name,boughtItems: items})
}
return acc;
}, []);
console.log(res);
Here it is as a function:
const data = [{
id: 1,
name: "Person 1",
boughtItems: [{
item: "Shoes",
currency: "GBP",
cost: 200
},
{
item: "Bag",
currency: "EUR",
cost: 300
}
]
},
{
id: 2,
name: "Person 2",
boughtItems: [{
item: "Shirt",
currency: "GBP",
cost: 13
},
{
item: "Jacket",
currency: "CAD",
cost: 150
}
]
}
]
function search(c){
return data.reduce((acc, {boughtItems, id, name}) => {
//filter through all items, and get those that match currency
const items = boughtItems.filter(({currency}) => currency === c);
//if there were some that matched, create the object with the items that match
if (items.length > 0) {
acc.push({id,name,boughtItems: items})
}
return acc;
}, []);
}
console.log(search("EUR"));
console.log(search("GBP"));
Upvotes: 1