I have to filter a array with same item and push results into another array
My array is
let list = [
{"name":"1000","properties":{"item":"1","unit":"DZN"}},
{"name":"2000","properties":{"item":"1","unit":"CTN"}},
{"name":"3000","properties":{"item":"2","unit":"DZN"}},
{"name":"4000","properties":{"item":"3","unit":"CTN"}}
]
I need corresponding name with condition item =1
in another array.
Result array will be similar to [{"name":"1000"}, {"name":"2000"}]
TIA
CodePudding user response:
If I understand correctly you want to create an array of names from records which have item values equal to 1. To do that you have to first filter an array to get only records which have desired value of item and then map those values to create an array that contains only values of the name attribute.
Here is an example:
let list = [
{"name":"1000","properties":{"item":"1","unit":"DZN"}},
{"name":"2000","properties":{"item":"1","unit":"CTN"}},
{"name":"3000","properties":{"item":"2","unit":"DZN"}},
{"name":"4000","properties":{"item":"3","unit":"CTN"}}
]
const filteredList = list.filter((e) => e.properties.item === "1").map((e) => e.name);
console.log(filteredList);
If this is a Vue3 reactive variable remember to add .value before filter() method to make it work