3

I have an array set out like this:

var newCircles = [{
    num: 0,
    name: "title0",
    x: 280,
    y: 200,
    color: "#555555",
    r: 60
},
{
    num: 1,
    name: "title1",
    x: 480,
    y: 200,
    color: "#555555",
    r: 80
}];

And I'm trying to push new set of information like this:

$(newCircles).push(', { num: "'+newCircles.length+'", name : "title "'+(newCircles.length)+'", x : "'+newCircles[chosenCircle].x+'", y : "'+newCircles[chosenCircle].y+'", color : "#7f38a7", r : "'+newCircles[chosenCircle].r+'" }');

But it's not working. Anyone have any suggestions?

Feeney
  • 357
  • 2
  • 4
  • 12
  • For starters, you have multiple references to `newCircles`. Is it a object or array? – OptimusCrime Feb 01 '12 at 14:23
  • 1
    have you tried pushing the object in rather than a string representation? Or save the contents of the object to a variable and add that e.g var obj = {num:...}; newCircles.push(obj); Also you seem to be using a jQuery object for $(newCircles).push whereas it's not a jquery object in your code shown - push also isn't a jquery function. Does firebug or anything show any errors? – TommyBs Feb 01 '12 at 14:23
  • It is referencing already existing items in the array 'newCircles'. – Feeney Feb 01 '12 at 14:26
  • No errors from firebug, i'll give the var obj idea a go. – Feeney Feb 01 '12 at 14:27

8 Answers8

6

you are pushing a string into the array. if you want to push another object into the array, then do so by

newCircles.push( {
  num: newCircles.length,
  name: 'title ' + newCircles.length,
  x: newCircles[chosenCircle].x,
  y: newCircles[chosenCircle].y,
  color : "#7f38a7",
  r: newCircles[chosenCircle].r
} );
Sirko
  • 72,589
  • 19
  • 149
  • 183
  • This worked perfect for me, a lot of people have added very similar if not identical solutions. Many thanks! – Feeney Feb 01 '12 at 14:35
2

You're using a string, but what you want to use is an object initializer (frequently called an object literal), just like you did when you initialized your array:

newCircles.push({
    num:   newCircles.length,                // Or you more likely want `newCircles.length + 1`
    name:  "title " + (newCircles.length),   // Again, probably +1
    x:     newCircles[chosenCircle].x,
    y:     newCircles[chosenCircle].y,
    color: "#7f38a7",
    r:     newCircles[chosenCircle].r
});

(There's also no reason for passing it through $().)

As when you initialized your array, the tokens to the left of the : are the property names, and the value of the expressions on the right will be assigned as those properties' values.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

Try this instead

newCircles.push( { num:   newCircles.length, 
                   name:  "title "+ newCircles.length, 
                   x:     newCircles[chosenCircle].x, 
                   y:     newCircles[chosenCircle].y, 
                   color: "#7f38a7", 
                   r:     newCircles[chosenCircle].r 
              }); 
aziz punjani
  • 25,586
  • 9
  • 47
  • 56
0

Why is that a string? And why are you trying to wrap newCircles with jQuery?

newCircles.push({ num: newCircles.length, x: newCircles[chosenCircle].x, ... });
James M
  • 18,506
  • 3
  • 48
  • 56
0

You don't need the leading comma when using the push method.

You're pushing the object directly onto the end of the array.

Also, there's no need to wrap the object up as a string.

$(newCircles).push({ num: "'+newCircles.length+'" [...]});
Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
0

Remove the $() around newCircles. You want the direct reference to that variable, not what jQuery returns when it looks it up.

schtever
  • 3,210
  • 16
  • 25
0

use as

<script>
    var newCircles = [ 
{
num: 0,
name: "title0",
x: 280,
y: 200,
color: "#555555",
r: 60
}, 
{
num: 1,
name: "title1",
x: 480,
y: 200,
color: "#555555",
r: 80
}
];

newCircles.push({"num":"10","name":"name","x":"100","y":"100","color":"color","r":"r"});
console.log(newCircles);
</script>
Dau
  • 8,578
  • 4
  • 23
  • 48
-1

Don't worry, javascript may not work correctly if the syntax is totally wrong;

newCircles.push({
    "num":newCircles.length,
    "a":"someval",
    "b":"some other val"
});
eyurdakul
  • 894
  • 2
  • 12
  • 29