I have a real time feed of names entering a JS function and I would like to create an object such as:
var renderObj= {
{"name": "test1", "size": 3938},
{"name": "test2", "size": 3812},
{"name": "test3", "size": 6714}
};
So renderObj is empty to start. Each time a name comes in, I need to check if it exists in the object. If it does, update the size.
Can anyone advise?
You can’t have unnamed items in an object. Use an array instead:
Loop through the array to check if a name exists:
Now check the result. I assume that you want to add an object if the name doesn’t exist yet. You didn’t say how you wanted the size updated, so I assumed that you wanted to add the new value to the previous: