I've got a JSON array like this:
0: {Id: "1", name: "Adam", Address: "123", userId: "i98"}
1: {Id: "2", name: "John", Address: "456"}
The second object in the array doesn't have a userId key.
How can I iterate through the array and add the userId key with a value of 0 if it's missing?
This is what I've attempted so far:
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();
}
}
However, I'm getting an error:
obj.find is not a function
Any suggestions on how to fix this issue and properly add the key value to the array would be appreciated.