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?
Add a new array of multidimensional array :push( ['testing', 'rust', 'c'] ); We display using the javaScript push method to add a new array to the outer array.
Javascript has no inbuilt support for multidimensional arrays, however the language is flexible enough that you can emulate this behaviour easily by populating your arrays with separate arrays, creating a multi-level structure.
If you just use below method to create a 3x3 matrix. Array(3). fill(Array(3). fill(0));
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.
Use []
:
cookie_value_add.push([productID,itemColorTitle, itemColorPath]);
or
arrayToPush.push([value1, value2, ..., valueN]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With