[javascript] javascript push multidimensional array

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?

This question is related to javascript arrays push

The answer is


Use []:

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

or

arrayToPush.push([value1, value2, ..., valueN]);

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);

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to push

Best way to "push" into C# array Firebase: how to generate a unique numeric ID for key? Why does Git tell me "No such remote 'origin'" when I try to push to origin? Unknown SSL protocol error in connection Git push rejected "non-fast-forward" How to add multiple files to Git at the same time git push to specific branch Declare an empty two-dimensional array in Javascript? What does '--set-upstream' do? fatal: 'origin' does not appear to be a git repository