Reputation:
I have an associative array with the following format:
var array = [{"id": "28", "name": "Josh"}, {"id": "17", "name": "Karl"}];
Is there any way to empty the key values of each array index?
I've done it this way, but I don't know if it's the best:
array.forEach(item => {
item.id = "";
item.name = "";
})
Upvotes: 1
Views: 158
Reputation: 1074465
The way you're doing it is fine if you really want to have blank strings as the id
and name
of all of the objects. A slightly more modern approach would use an ES2015+ for-of
loop, and you can use a single statement to set both values if you like:
for (const item of array ) {
item.id = item.name = "";
}
But again, your forEach
loop is fine, and works in environments that have forEach
but don't have for-of
.
Upvotes: 2