Looking to efficiently parse a large JSON file based on its values.
This is an excerpt from my extensive JSON code (with over 1000 entries).
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"
}
];
In order to filter the JSON object above, I created the following filtering function.
function getUsersBasedOnIDs(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);
}
}
Here's how you can call the function:
getUsersBasedOnIDs("");
getUsersBasedOnIDs("10261,10165");
getUsersBasedOnIDs("9999xx");
Identified issues with the current implementation include:
The function is not compatible with IE 11 and earlier versions
When calling the function with parameters like
usersBasedOnIDs("10261,10165");
orusersBasedOnIDs("10261");
, it returns multiple JSON outputs instead of one specific output{UserId: 10405, FullName: "test value", CustomerId: "10261"} {UserId: 10416, FullName: "shoze ahh", CustomerId: "1"} {UserId: 10244, FullName: "kajal man", CustomerId: "10"}
Ideally, the expected output should be just:
{UserId: 10405, FullName: "test value", CustomerId: "10261"}
To address these issues and achieve the desired results, revisions to the function are necessary.