I have a json array as:
0: {Id: "1", name: "Adam", Address: "123", userId: "i98"}
1: {Id: "2", name: "John", Address: "456"}
Now in the above array, the second one has no userId key available.
How can I loop through the above array and check if no userId key available then add the key with value 0. For eg add userId: "0"
What I have tried is the following:
let jsonData = JSON.parse(JSON.stringify(userData));
for (const obj of jsonData) {
var hasId = false;
let objId = obj.find((o, i) => {
if (o.userId === "userId") {
hasId = true;
}
});
if (!hasId) {
obj.push();
}
}
But this gives me error:
obj.find is not a function
Any inputs to resolve my issue and push the key value to the array.