49

I've got something like that:

    var valueToPush = new Array();
    valueToPush["productID"] = productID;
    valueToPush["itemColorTitle"] = itemColorTitle;
    valueToPush["itemColorPath"] = itemColorPath;

    cookie_value_add.push(valueToPush);

the result is [];

what am i do wrong?

sinini
  • 1,403
  • 3
  • 19
  • 27
  • Where did you see "the result is []"? – Yaakov Shoham Oct 24 '11 at 18:54
  • 1
    If you want to create an array use [] literal notation instead of new Array. Also, if you want to store general key-value pairs use normal objects instead of arrays: `var toPush = {}; toPush.productId = ...` – hugomg Oct 24 '11 at 19:20
  • the result was in my cookie i stored the value into, and with more values storing, more [] appeared... – sinini Oct 25 '11 at 07:35

3 Answers3

84

Arrays must have zero based integer indexes in JavaScript. So:

var valueToPush = new Array();
valueToPush[0] = productID;
valueToPush[1] = itemColorTitle;
valueToPush[2] = itemColorPath;
cookie_value_add.push(valueToPush);

Or maybe you want to use objects (which are associative arrays):

var valueToPush = { }; // or "var valueToPush = new Object();" which is the same
valueToPush["productID"] = productID;
valueToPush["itemColorTitle"] = itemColorTitle;
valueToPush["itemColorPath"] = itemColorPath;
cookie_value_add.push(valueToPush);

which is equivalent to:

var valueToPush = { };
valueToPush.productID = productID;
valueToPush.itemColorTitle = itemColorTitle;
valueToPush.itemColorPath = itemColorPath;
cookie_value_add.push(valueToPush);

It's a really fundamental and crucial difference between JavaScript arrays and JavaScript objects (which are associative arrays) that every JavaScript developer must understand.

Makyen
  • 31,849
  • 12
  • 86
  • 121
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Arrays are objects too. Therefore, in the original code the properties are there, and it's not just `[]`. – Yaakov Shoham Oct 24 '11 at 18:55
  • @Y. Shoham, yes arrays are objects. It's just that the `[]` property has a special meaning for them. You can only use 0 based integers which is at the root of the OPs problem. – Darin Dimitrov Oct 24 '11 at 19:07
14

Use []:

cookie_value_add.push([productID,itemColorTitle, itemColorPath]);

or

arrayToPush.push([value1, value2, ..., valueN]);
Andrew
  • 5,839
  • 1
  • 51
  • 72
user2560779
  • 141
  • 1
  • 2
5

In JavaScript, the type of key/value store you are attempting to use is an object literal, rather than an array. You are mistakenly creating a composite array object, which happens to have other properties based on the key names you provided, but the array portion contains no elements.

Instead, declare valueToPush as an object and push that onto cookie_value_add:

// Create valueToPush as an object {} rather than an array []
var valueToPush = {};

// Add the properties to your object
// Note, you could also use the valueToPush["productID"] syntax you had
// above, but this is a more object-like syntax
valueToPush.productID = productID;
valueToPush.itemColorTitle = itemColorTitle;
valueToPush.itemColorPath = itemColorPath;

cookie_value_add.push(valueToPush);

// View the structure of cookie_value_add
console.dir(cookie_value_add);
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390