-2

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.

JasonA
  • 241
  • 2
  • 5
  • 13
  • 1
    `object.forEach((o) => { o.username = 'clientname'; })`. – Marty Jul 31 '16 at 12:41
  • What's the issue? It involves 2 things: iterating the Array and assigning the property. If you don't know how to do either of those, then you need to read beginner materials before asking questions. And why do you want to avoid loops? –  Jul 31 '16 at 12:43
  • Thanks Marty. Squint, I'm typically coding in R and have learned to avoid loops as its inefficient in that language. New to javascript and am finding my way around. – JasonA Jul 31 '16 at 12:47
  • Thanks alexi2, please notice that my question requests the avoidance of loops. – JasonA Jul 31 '16 at 12:55
  • 1
    If the question requests the avoidance of loops, you shouldn't have accepted the answer that you did. The `.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. –  Jul 31 '16 at 12:56

2 Answers2

7

Iterate over elements using Array#forEach and set username property.

var object = [{
  account: 2423,
  user: 1564
}, {
  account: 1235,
  user: 1564
}];

object.forEach(function(v) {
  v.username = 'clientname';
})

console.log(object);

Even you can use the simple for loop.

var object = [{
  account: 2423,
  user: 1564
}, {
  account: 1235,
  user: 1564
}];

for (var i = 0; i < object.length; i++) {
  object[i].username = 'clientname';
}

console.log(object);

FYI : Without loop it may be more complex and inefficient. If limited numbers of elements are there then you can update it with its index although as @squint suggested recursion can be used but I don't think all those are necessary here since it's a job of a simple loop .

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
7

You can use Array.map method to get the result. Here's code

var object = [
    {account:2423, user:1564},
    {account:1235, user:1564}
];

object.map(function(entry) {
    entry.username = "clientname";
    return entry;
});
Bikas
  • 2,709
  • 14
  • 32
  • Bikas, would you say array.map is more or less efficient than forEach or a for loop? – JasonA Jul 31 '16 at 12:48
  • This explains better than I could https://ryanpcmcquen.org/javascript/2015/10/25/map-vs-foreach-vs-for.html Essentially, map is fastest of all – Bikas Jul 31 '16 at 12:56
  • Good artical, thanks Bikas. Switching yours to the correct answer. – JasonA Jul 31 '16 at 13:00
  • 2
    @JasonA: Why? It's some random dude's blog and is making spurious claims. In my tests, `.map()` is slower, and you're still not avoiding loops like your question states. –  Jul 31 '16 at 13:04
  • 5
    `map` is not appropriate here, one should use `forEach` for this kind of work – georg Jul 31 '16 at 13:06
  • @georg Care to explain how `map` is not appropriate here when it's specifically made for these scenarios? You can always see the reference in MDN which says the same – Bikas Jul 31 '16 at 13:09
  • 4
    @BikasVaibhav: `map` generates a new array from an existing one. Generally, one should avoid side effects when using `map`. To apply a function with side effects to an array, there's `forEach`. – georg Jul 31 '16 at 13:18
  • 1
    @BikasVaibhav: It's specifically made to build a new array from an existing one. Why are you using a method that builds a new array that you're just going to throw away? Makes it look like you have a bug in your code. If I came across that, I'd be asking why the array isn't being retained. –  Jul 31 '16 at 13:18
  • That's agreeable and I missed that. But `map` is clearly one of the solutions. – Bikas Jul 31 '16 at 13:19
  • @BikasVaibhav: yes, you could use `map` in a manner that leaves the original array untouched, e.g. `newList = oldList.map(x => Object.assign({}, x, {newProp: '...'}));` – georg Jul 31 '16 at 13:21
  • @Bikas, Thx for this great answer. I needed the logic but I used `forEach` instead of `map`. – Mo1 Jan 06 '21 at 10:12