I am working in node without angular or underscore and am looking for an efficient plain javascript answer (perhaps avoiding loops). I have the following array of JSON objects:
object = [
{account:2423, user:1564},
{account:1235, user:1564}
]
I want to add the following to each array within the object:
username:"clientname"
So that it looks like this in the end:
object = [
{account:2423, user:1564, username:"clientname"},
{account:1235, user:1564, username:"clientname"}
]
This question requests the avoidance of loops.
object.forEach((o) => { o.username = 'clientname'; })..forEach()method is ultimately just looping and invoking the function. If anything, it'll likely be slower. If you want to avoid loops, you can use recursion, and find that you won't have any performance gains.