I have a JSON (My JSON) in which I want to filter out all the records with myRank equal to null. I am using array filter but not sure why it's not giving the desired output, instead I am getting a blank array. I want a new independent result. Don't want to alter the original data.
My JSON:
ar = {
"test": [
{
"id": "abc-123-def",
"myResults": [
{
"distancetoriver": 2308.30,
"profId": "klm-rtu-0987",
"marketId": "51534",
"myRank": 1
},
{
"distancetoriver": 5612.29,
"profId": "abc-def-0987",
"marketId": "51535",
"myRank": null
}
]
},
{
"id": "pqr-053-klm",
"myResults": [
{
"distancetoriver": 1978.61,
"profId": "sdc-poo-0968",
"marketId": "51536",
"myRank": 2,
},
{
"distancetoriver": 15687.25,
"profId": "foo-soo-0945",
"marketId": "51539",
"myRank": null,
}
]
},
{
"id": "qwe-053-qrt",
"myResults": [
{
"distancetoriver": 3125.59,
"profId": "red-ikj-0968",
"marketId": "51542",
"myRank": null,
},
{
"distancetoriver": 5489.68,
"profId": "frt-hyu-0945",
"marketId": "51598",
"myRank": null,
}
]
}
]
}
Desired Output:
{
"test": [
{
"id": "abc-123-def",
"myResults": [
{
"distancetoriver": 2308.30,
"profId": "klm-rtu-0987",
"marketId": "51534",
"myRank": 1
}
]
},
{
"id": "pqr-053-klm",
"myResults": [
{
"distancetoriver": 1978.61,
"profId": "sdc-poo-0968",
"marketId": "51536",
"myRank": 2,
}
]
},
{
"id": "qwe-053-qrt",
"myResults": [
]
}
]
}
My Code so far:
x = ar['test'].filter(function (obj) {
obj.myResults.filter(function (item) {
console.log(item.myRank)
return item.myRank != null
});
});
console.log(x)