i have an object with more than 600 entries. shown below is a sample of it. i would like to know the fastest way to iterate through that object and obtains all values related to the key `eppo' and save these values in another array
Also I would like to know also how to prevent duplicates please?i mean if the there are several entried with key eppo and have the same value, then only one of them is added to the list. object:
[
{
"pppId": "024675-00",
"eppo": "PAVSA"
},
{
"pppId": "024675-00",
"eppo": "ALLCE"
},
{
"pppId": "024675-00",
"eppo": "BRSOC"
},
{
"pppId": "024675-00",
"eppo": "APUGD"
},
{
"pppId": "024675-00",
"eppo": "PRNPN"
},
{
"pppId": "024675-00",
"eppo": "PYUCO"
},
]
CodePudding user response:
Use Array.map
to obtain the values and pass them to a Set
to prevent duplicates:
const result = [...new Set(data.map(item => item.eppo))];