I have a collection of objects, each structured as follows:
{
"myFilters": [
{
"isMatch": false,
"filters": [
{
"id": "aaaaaa",
"version": "v1"
},
{
"id": "kk",
"version": "v1"
}
]
}
],
"randomAttr1": null,
"randomAttr2": []
}
Assume the above is an object within the result
list.
My aim is to extract all the versions and append them as values to a new element relevant_versions
, but with the condition that both the Id and version must match the URL parameters. Here's what I've tried:
for (let f of result) {
f.relevant_versions = f.myFilters.filter(x=>x.filters
.filter(item=>(item.id == this.$route.params.filterId && item.version == this.$route.params.version))
.map(fid => fid.version))
}
However, instead of just the versions, the entire myFilters
element is being added. I believe I may be making a simple mistake here.
What would be the correct way to populate relevant_versions
?
Edit: The desired output should resemble:
{
"myFilters": [
{
"isMatch": false,
"filters": [
{
"id": "aaaaaa",
"version": "v1"
},
{
"id": "kk",
"version": "v1"
}
]
}
],
"randomAttr1": null,
"randomAttr2": [],
"relevant_versions":["v1", "v1"]
}
An example route could be localhost:8080/filters/kk/v1
. In this case, kk
corresponds to this.$route.params.filterId
and v1
to this.$route.params.version
.