Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can i filter a array in Vue3

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Result array will be similar to [{1000, 2000}]

TIA

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading