-4

I have object, and for each element i can generate a random number. I use this simple code:

var ss = [];
var a = [{ i: 5, _r: 0 }, { i: 6, _r: 0 }, { i: 7, _r: 0 }];
var b = function (a) { for (var i = 0; i < a.length; i++) { a[i]._r = Math.random(); } return a; };
for (j = 0; j < 5; j++) { ss.push(b(a)); };
console.log(ss);

After log array i obtain same object for each array. Resp. ss[0][0]._r = ss[1][0]._r = ss[2][0]._r. But _r key is Math.random, this is not right. I would like have in each key _r a random number.

And for searching solution, using the function b is necessary! Thanks

MarTic
  • 665
  • 3
  • 10
  • 27
  • 1
    Your just overwriting the same `a` properties over and over again. Clone it somewhere along the way. – Sirko Mar 13 '15 at 20:01
  • This question is an exact duplicate of [your former question](http://stackoverflow.com/q/29032962/1048572). Please don't do that, [edit] your question instead of deleting and re-posting it. – Bergi Mar 14 '15 at 22:49

1 Answers1

1

JavaScript passes arguments by reference, so whenever you pass the array to your function you pass the same instance and add it to the result array. Hence they are all the same.

An easy solution would be to build a new array inside of b and return that.

var ss = [];
var a = [{ i: 5, _r: 0 }, { i: 6, _r: 0 }, { i: 7, _r: 0 }];
var b = function (x) { 
    var result = [];
    for (var j = 0; j < x.length; j++) {
        result.push({ i : x[j].i, _r : Math.random()} );
    }
    return result; 
};
for (j = 0; j < 5; j++) { ss.push(b(a)); };
console.log(ss);

I'm not quite sure which purpose the other property serves, but you can also pass some input array and use it for the calculation. The important point is to create a new array.

MarTic
  • 665
  • 3
  • 10
  • 27
SGD
  • 1,676
  • 14
  • 18
  • Have you please a edited examle? Now i'm over the mind. Thanks – MarTic Mar 13 '15 at 20:03
  • 1
    Arguments are not really [passed by reference](http://stackoverflow.com/a/3638034/2476755); they're passed by value (but you can modify a reference to an object). – royhowie Mar 13 '15 at 20:16
  • Sorry, this example for solution is not functional – MarTic Mar 13 '15 at 20:17
  • My apologies, it was typed on my cell. Now it should do. – SGD Mar 13 '15 at 20:20
  • And what if i would like have in array object like in ''a'' parameter [{}, {}]? Same result like in my code. – MarTic Mar 13 '15 at 20:22
  • Then you pass it and copy all properties over to the target. I updated the example, it could of course be done in a loop if the are a lot of properties. – SGD Mar 13 '15 at 20:25
  • Yes, that's it, but what if i have large object? Is any solution, where i don't use this `result.push({ i : x[j].i, _r : Math.random()} );` and use just `result._r = Math.random()` ? – MarTic Mar 13 '15 at 21:00
  • 1
    You can write a function which creates a deep copy of the input object first and then you assign your random value. – SGD Mar 13 '15 at 21:04
  • I found solution, its **Object.clone()** – MarTic Mar 13 '15 at 21:24