I need to filter a huge JSON file based on its value.
this is my JSON code(this is part of my JSON object, In real behaviour, its have more than 1000).
var jsonObject = [
{
"UserId":10259,
"FullName":"hello world",
"CustomerId":"10165"
},
{
"UserId":10405,
"FullName":"test value",
"CustomerId":"10261"
},
{
"UserId":10400,
"FullName":"mark ant",
"CustomerId":"10161"
},
{
"UserId":16224,
"FullName":"jhon cena",
"CustomerId":""
},
{
"UserId":10416,
"FullName":"shoze ahh",
"CustomerId":"1"
},
{
"UserId":10244,
"FullName":"kajal man",
"CustomerId":"10"
}
];
To filter the above JSON object, I used the following filter function.
function usersBasedOnIDs(CustomerIds) {
if (CustomerIds == "") {
console.log(jsonObject);
} else if (CustomerIds == "9999xx") {
let result = jsonObject.filter(c => c.CustomerId == "");
console.log(result);
} else {
let result = jsonObject.filter(c => c.CustomerId != "" && CustomerIds.includes(c.CustomerId));
console.log(result);
}
}
How I call the function
usersBasedOnIDs("");
usersBasedOnIDs("10261,10165");
usersBasedOnIDs("9999xx");
There are a couple of problems with my code,
This function not working with IE 11 an early version
And another thing is when I call the function as
usersBasedOnIDs("10261,10165");orusersBasedOnIDs("10261");, its returns follwing JSON output{UserId: 10405, FullName: "test value", CustomerId: "10261"} {UserId: 10416, FullName: "shoze ahh", CustomerId: "1"} {UserId: 10244, FullName: "kajal man", CustomerId: "10"}
But my expectation is, only this
{UserId: 10405, FullName: "test value", CustomerId: "10261"}
How can change the function to avoid those two problems