-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.

5
  • 1
    object.forEach((o) => { o.username = 'clientname'; }). Commented Jul 31, 2016 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? Commented Jul 31, 2016 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. Commented Jul 31, 2016 at 12:47
  • Thanks alexi2, please notice that my question requests the avoidance of loops. Commented Jul 31, 2016 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. Commented Jul 31, 2016 at 12:56

2 Answers 2

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;
});
Sign up to request clarification or add additional context in comments.

11 Comments

Bikas, would you say array.map is more or less efficient than forEach or a for loop?
@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.
map is not appropriate here, one should use forEach for this kind of work
@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.
@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.
|
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 .

1 Comment

The for loop is the fastest to perform I believe.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.