-1

My program is returning values in single array: ["addEcommerceItem", "hat", 29.99, "addEcommerceItem", "belt", 19.99];

I am trying to acheive (["addEcommerceItem", "hat", 29.99], ["addEcommerceItem", "belt", 19.99]);

Can anyone make suggestion

products = [
    ["hat", 29.99],
    ["belt", 19.99]
]

var testArray = new Array();

for(i in products){
    testArray.push('addEcommerceItem');
    testArray.push(products[i]);
}

var json = JSON.stringify(testArray);

<script type="text/javascript">
  var _paq = _paq || [];
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  _paq.push(json);
</script>
Jgunzblazin
  • 137
  • 1
  • 2
  • 11
  • You really should remove those `productid =` and `price =` assignments from your array literal – Bergi Apr 23 '15 at 17:52

3 Answers3

2

First of all, don't use for in enumerations on arrays.

Now, there are two ways of approaching the result you want. First, you can simply alter the existing array by prepending that item to every of them:

for (var i=0; i<products.length; i++)
    products[i].unshift("addEcommerceItem");

console.log(JSON.stringify(products));

If that's not what you want, you would construct a new array rather:

var _paq = […]; // somewhere

for (var i=0; i<products.length; i++)
    _paq.push(["addEcommerceItem"].concat(products[i]));

console.log(JSON.stringify(_paq));

Instead of the concat, you could of course use a second loop:

var _paq = […]; // somewhere

for (var i=0; i<products.length; i++) {
    var product = products[i],
        temp = [];
    temp.push("addEcommerceItem";
    for (var j=0; j<product.length; j++)
        temp.push(product[j]);
    _paq.push(temp); // push each array individually
}

console.log(JSON.stringify(_paq));
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • All I want is inside the _paq.push( [ ], [ ] ); NOT _paq.push( [ [ ], [ ] ] ); Even though "addEcommerceItem" appears to be a string it is a function from another program that includes the wrapper [ ]. That is why nested arrays will not work in this case. – Jgunzblazin Apr 23 '15 at 22:25
  • Please look again. What I my code does **is** `_paq.push( [ ], [ ] )`! There is no other nesting except `_paq` itself! It's only the wrapper array that comes with your "function"! – Bergi Apr 23 '15 at 23:03
  • Please look at the original code above. What you are saying is not true. Post on JS Bin. _paq is an empty array [ ]. Your code outputs [ [ ], [ ] ]. (Try it!) So in theory what you are actually producing is [ [ [ ], [ ] ] ]. That is why the output needs to be [ ], [ ] only so that it _paq.push( [ ], [ ] ); like so. – Jgunzblazin Apr 24 '15 at 00:16
  • as you mentioned above I don't think JSON.stringify will work here because it will never show two structures next to each other – Jgunzblazin Apr 24 '15 at 00:21
  • Here is an example of what you are essentially outputting: [JS BIN](http://jsbin.com/cipaneyada/1/edit?html,console) – Jgunzblazin Apr 24 '15 at 00:41
  • I posted your code [here](http://jsbin.com/necofiduga/1/edit?html,console) so you can see what the issue is. I changed your variable name. – Jgunzblazin Apr 24 '15 at 00:53
  • Please read my answer again. It doesn't use a separate `emptyArray`, but pushes to `_paq` directly! – Bergi Apr 24 '15 at 12:16
  • Hi Bergi, see [JS BIN](http://jsbin.com/bidoqihoxu/1/edit?html,console) here. A circular object Array is returned.. – Jgunzblazin Apr 24 '15 at 15:14
  • No wonder when you do `_paq.push(_paq);`… but that's not what the code in my answer does. – Bergi Apr 24 '15 at 15:21
  • Yeah sorry for the confusion. This the way I need it to work unfortunately. I need that value in the _paq.push( ); to be just [ ], [ ]. It's quite a pain! – Jgunzblazin Apr 24 '15 at 15:56
  • @Jgunzblazin: there is no such value that acts as two values. You need to push in a loop, or use `apply`. What are the actual constraints for your script that pushes to `_paq`, and where do they come from? – Bergi Apr 24 '15 at 21:28
  • yes that's why I changed your variable name in the previous JS BIN above. The constraints are to push non-nested arrays. The secondary script is provided from a 3rd party collecting information. That's why "addEcommerceItem" works as a function. [Look here](http://piwik.org/docs/ecommerce-analytics/#ecommerce-tracking) to see the documentation. – Jgunzblazin Apr 24 '15 at 22:32
  • Oh come on that can't be so hard to understand: http://jsbin.com/feyuyojuyi/2/edit – Bergi Apr 25 '15 at 09:18
0

To achieve your desired output, I've modified the for-loop to splice in the additional value to the products array instead of pushing it to the testArray.

var testArray = new Array();

for (var i=0; i < products.length; i++){
    products[i].splice(0,0,'addEcommerceItem');
    testArray.push(products[i]);
}

A JS Bin demonstrating the results can be seen here.

OhBeWise
  • 5,350
  • 3
  • 32
  • 60
  • hmm, no good, this returns: [Array[0], Array[0], Array[2], "addEcommerceItem", Array[2]] – Jgunzblazin Apr 23 '15 at 15:33
  • 1
    I've created [this JS Bin](http://jsbin.com/foyucidove/1/edit?html,console) to demonstrate the results I'm seeing. Can you show me how you are getting your results? – OhBeWise Apr 23 '15 at 16:12
  • 1
    You are right. I keyed it incorrectly. However this produces multi-dimensional array:[ ["addEcommerceItem", "hat", 29.99], ["addEcommerceItem", "belt", 19.99]]; I am trying to acheive: ( ["addEcommerceItem", "hat", 29.99], ["addEcommerceItem", "belt", 19.99]); – Jgunzblazin Apr 23 '15 at 16:53
  • so basically ( [ ], [ ] ); – Jgunzblazin Apr 23 '15 at 17:07
  • Ah, I see. I misread your OP thinking multi-dimensional was your intention. That was my oversight. – OhBeWise Apr 23 '15 at 20:43
  • @Jgunzblazin I've modified my original answer to hopefully correct the issue. – OhBeWise Apr 23 '15 at 21:15
  • your JS Bin returns: [["addEcommerceItem", "hat", 29.99], ["addEcommerceItem", "belt", 19.99]] which is still nested. – Jgunzblazin Apr 23 '15 at 21:38
0

Just write what you want to happen: replace each sub-array with its elements preprended by "addEcommerceItem". You could use concat for that:

function prepend(item) { return ["addEcommerceItem"] . concat(item); }

Or, if you prefer,

function prepend(item) { item.unshift("addEcommerceItem"); return item; }

Then it's just

function transform(products) { return products . map(prepend); }

Sample usage:

>> products = [ ["hat", 29.99], ["belt", 19.99] ]
>> transform(products)
<< [ ["addEcommerceItem","hat",29.99], ["addEcommerceItem","belt",19.99] ]

To call _paq.push with each item in the resulting array as one argument, then

_paq.push.apply(_paq, transform(products))

In ES6:

var prepend   = item     => ["addEcommerceItem", ...item];
var transform = products => products . map(prepend);

_paq.push(...transform(products));
  • what a headache for me. I am not trying to create nested array. I need to push separate array into variable like so: _paq.push( [ ], [ ] ); – Jgunzblazin Apr 23 '15 at 20:46
  • Your expression "push separate array into variable" is extremely hard to understand. Do you mean to do `_pag.push.apply(_paq, transform(products))`? –  Apr 23 '15 at 20:50
  • Bergi hit the nail on the head. I am simply trying to add array objects to _paq.push( ); – Jgunzblazin Apr 23 '15 at 20:53