I have an array of objects in javascript, I am creating a new array from it but only with some properties. What I need is to filter the unique objects and eliminate the repeated ones, I’m trying to do it in the same function but I don’t know if it’s possible
This is the array:
let filters = [
{
"model":"audi",
"year":2012,
"country":"spain"
},
{
"model":"bmw",
"year":2013,
"country":"italy"
},
{
"model":"audi",
"year":2020,
"country":"spain"
}
]
This is the function:
function filterObject(data: any) {
let result = data.map((item: any) => {
return {
hub: {
country: item.country,
model: item.model
}
}
})
return result
}
This is what I get:
[
{
"hub":{
"country":"spain",
"model":"audi"
}
},
{
"hub":{
"country":"italy",
"model":"bmw"
}
},
{
"hub":{
"country":"spain",
"model":"audi"
}
}
]
This is what I need:
[
{
"hub":{
"country":"spain",
"model":"audi"
}
},
{
"hub":{
"country":"italy",
"model":"bmw"
}
}
]
2
Answers
Try this updated function
itz working as per your expectation.
You can use
Map
to ensure only unique items:Playground