Reputation: 43
I have an array like below and I want to extract objects and sub-array and create a new array if the selected object value is true
menu: [
{
category_name: "snacks",
selected: true
items: [
{
item_name: "burger",
price: 00,
bestseller: false
}
]
},
{
category_name: "tiffins",
selected: false
items: [
{
item_name: "idly",
price: 00,
bestseller: false
}
]
},
]
i want new array would be like this
new_menu: [
{
category_name: "snacks",
items: [
{
item_name: "burger",
price: 00,
bestseller: false
}
]
},
]
Upvotes: 3
Views: 60
Reputation: 3629
var new_menu = menu.filter(function (el) {
return el.sellected;
});
If you want to exclude sellected
key then:
var new_menu = menu.filter(function (el) {
return el.sellected;
}).map(function(elem) {
return {'category_name': elem.category_name, 'items': elem.items}
});
Upvotes: 3
Reputation: 11979
You can try this:
const new_menu = menu
// Get the selected items
.filter(m => m.sellected)
// Get rid of the `sellected` property
.map(({ sellected, ...m }) => m)
Upvotes: 5