51

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?

asked Oct 24, 2011 at 18:42
3
  • Where did you see "the result is []"? Commented Oct 24, 2011 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 = ... Commented Oct 24, 2011 at 19:20
  • the result was in my cookie i stored the value into, and with more values storing, more [] appeared... Commented Oct 25, 2011 at 7:35

3 Answers 3

85

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
33.6k12 gold badges94 silver badges128 bronze badges
answered Oct 24, 2011 at 18:46
Sign up to request clarification or add additional context in comments.

2 Comments

Arrays are objects too. Therefore, in the original code the properties are there, and it's not just [].
@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.
16

Use []:

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

or

arrayToPush.push([value1, value2, ..., valueN]);
Andrew
6,7862 gold badges61 silver badges81 bronze badges
answered Jul 8, 2013 at 12:48

Comments

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);
answered Oct 24, 2011 at 18:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.