0

Newbie here...be nice.

I have an empty object that will get pushed into an array.

listView = {};

I add properties to it.

listView.code = code;
listView.description = description;

I push the results object into an array.

listy.push(listView);

Each time I enter a new selection in step #2 it overwrites the object instead of adding the new object properties to the array. It also increments the index by one, so it just repeats...

[{"code":"I77.812","description":"Thoracoabdominal Aortic Ectasia"}]
[{"code":"I77.811","description":"Abdominal Aortic Ectasia"},{"code":"I77.811","description":"Abdominal Aortic Ectasia"}]
[{"code":"I06.1","description":"Rheumatic aortic insufficiency"},{"code":"I06.1","description":"Rheumatic aortic insufficiency"},{"code":"I06.1","description":"Rheumatic aortic insufficiency"}]

The array should contain three different objects. But instead it has three copies of the newly added one...

How should I be adding the new choice objects so that they don't get overwritten?

httpex
  • 1
  • 3
  • How are you adding the three new objects? Can you put the code that you are using to do that? – TheDude Aug 28 '15 at 06:33
  • First mistake: var listView= {}; – gview Aug 28 '15 at 06:34
  • javascript object are passed by refference. So once the list object value is changed. It also reflects inside the listy array. You may create the list object each time inside the loop and as var list = {}, and push the list object also inside the loop. – Abhisek Malakar Aug 28 '15 at 06:36

4 Answers4

1

You are always adding a reference to the same object, and changing that same object, instead of adding new objects. See this:

var a = [];
var o = {};
for (var i = 0; i < 5; i++) {
  o.id = i;
  a.push(o);
}
a
// => [{"id":4},{"id":4},{"id":4},{"id":4},{"id":4}]

But

var a = [];
for (var i = 0; i < 5; i++) {
  var o = {};
  o.id = i;
  a.push(o);
}
a
// => [{"id":0},{"id":1},{"id":2},{"id":3},{"id":4}]

The difference is, the second code always makes a new object that is distinct from all other objects already in the array.

As a metaphor, imagine a theatre director in casting. He turns to an actor, says "You... you'll be Romeo.". Then he looks at the same actor, says "You... you'll be Mercutio. Here, Mercutio, take this sword. Romeo... who told you to get a sword?!?" completely failing to realise that, if Romeo and Mercutio are the same person, if one of them picks up a sword, the other does it too.

Amadan
  • 191,408
  • 23
  • 240
  • 301
0

try this :

listy.push({
    code:listView.code,
    description : listView.description
})

In my code I have used pass by value.

In your code , you are using Objects which are passed by reference .

You are adding same reference again and again so at the end you will get an array having all the values of same object .

To understand more about pass by value and pass by reference you can reffer this link :

Pass Variables by Reference in Javascript

Community
  • 1
  • 1
bob
  • 4,595
  • 2
  • 25
  • 35
  • This worked! Didn't even know you could do that! Thanks man! – httpex Aug 28 '15 at 06:40
  • That's great !! I will update by answer to make it more clearer :) – bob Aug 28 '15 at 06:42
  • Well yes it works, but what do you gain by first assigning individual object members to an intermediate object? You might just as well set the variables values directly in that code. Hopefully you understand the scoping issue now, as illustrated in Amadan's code. This is another way of doing the assignment, but it's not really what you asked about. – gview Aug 28 '15 at 06:44
  • I totally get the scoping issue from Amadan's explanation. In my code I would have needed to add a for loop and put the object in it just like he did. – httpex Aug 31 '15 at 22:34
0

Seeing as you declared yourself a 'newbie' i figured i'd take a bit more time explaining. When you push an object to an array, you don't copy the object. You just tell the array where to find the object (a reference). If you push the same object 3 times, the array just has 3 indexes at which it finds the same object. There's several ways around this, the easiest being that you declare the variable inside the loop

for (var i=0;i<3;i++){
    var listView = {};
    listView.id = i;
    listy.push(listView);
}

This way listView is a different reference each time. The other way is to create a new object when you push

listy.push({id:listView.id, description:listView.description});

which works because simple variables are 'copied' into the array and not referenced.

DrunkWolf
  • 994
  • 1
  • 6
  • 18
0

your assignment of the properties of an object are simply replacing the existing properties. wh en you push the object in the array by name, you are push a reference to the object and not a value. This is why all the elements in the array are the same. You need to create a new object every time you push. Something like this should work for you.

listy.push({code:code, description:description});
Dale Corns
  • 225
  • 1
  • 6