-3

This comes through GTM as an object that I have to convert to an array, so that I can loop through it.

I've written a forEach statement for it, but I can't seem to format this properly to work.

(full disclosure, I'm not a developer, I'm just trying to muddle my way through GTM)

I've tried using Object.entries(obj), but the result was multiple arrays.

{
  0: {
    category_id: '',
    cart_entry_id: 0,
    quantity: 3,
    product_id: '678294',
    unit_price: 5.29,
    product_name: 'Office Depot® Brand 8-Pocket Poly Organizer, Assorted Colors (No Color Choice)',
  },
  2: {
    category_id: '543867',
    cart_entry_id: 2,
    quantity: 1,
    product_id: '448906',
    unit_price: 34.99,
    product_name: 'Realspace® All-Pile Studded Chair Mat, 36" x 48";, Clear',
  },
  3: {
    category_id: '543867',
    cart_entry_id: 3,
    quantity: 1,
    product_id: '493876',
    unit_price: 179.99,
    product_name: 'Realspace® MFTC 200 Mesh Multifunction Ergonomic Mid-Back Task Chair, Black',
  }
}
Dimitri Kopriwa
  • 13,139
  • 27
  • 98
  • 204
J.R. Dunn
  • 3
  • 2
  • 3
    Could you please show a sample of what you expect in the array? – Dimitri Kopriwa Oct 03 '19 at 19:17
  • 2
    Welcome J.R. Dunn. Always be sure to give a [minimal example](https://stackoverflow.com/help/minimal-reproducible-example). In your case you just have arbitrary objects that need to turn into an array. What is in those doesn't matter, so trimming it down to a smaller sample will make it easier to understand the question and make it more likely you'll get good responses. – samanime Oct 03 '19 at 19:17
  • Just apply `Object.values(input)` – trincot Oct 03 '19 at 19:19
  • Thanks for the comments, I'll try to minimize it. – J.R. Dunn Oct 03 '19 at 19:20
  • What do you mean by "without arrow functions"? Those are a standard feature in ES6, but you can always use the old `function() { ... }` form. – tadman Oct 03 '19 at 19:22
  • Google Tag Manager doesn't support ES6, so I can't use them. That's why I had to post the question. All answers I found included those. – J.R. Dunn Oct 03 '19 at 19:27
  • Could you please when you edit your message keep the formating I have done like 3 times instead of redoing the same mistakes? I did vote up your question but now I believe you don't deserve it. – Dimitri Kopriwa Oct 03 '19 at 19:29
  • I'm really sorry Dimitri, this is my first time posting here. I'm just trying to figure this stuff out. I'll delete the question. This was a mistake. – J.R. Dunn Oct 03 '19 at 19:32
  • Never mind. I can't delete. I'll keep looking for answers elsewhere. Thank you all for your time, and I'm sorry I couldn't explain my problem better. – J.R. Dunn Oct 03 '19 at 19:34

3 Answers3

1

If you just have any object like this:

{
  0: 'some data',
  1: 'some data',
  2: 'some data'
}

You can easily turn it into an array with this:

const input = {
  0: 'some data',
  1: 'some data',
  2: 'some data'
};

const output = Object.values(input);

console.log(output);

If you need to ensure the keys are always in the same order (which you probably should) you can add a sort on it first.

const input = {
  0: 'some data',
  1: 'some data',
  2: 'some data'
};

// entries turns the object into an array of arrays with each
// entry being [key, value].
const output = Object.entries(input)
  .sort((a, b) => a[0] - b[0])
  .map(i => i[1]);

console.log(output);

Since you asked without array functions, just change the arrow functions to not arrow functions (though not sure why you'd want to):

const input = {
  0: 'some data',
  1: 'some data',
  2: 'some data'
};

// entries turns the object into an array of arrays with each
// entry being [key, value].
const output = Object.entries(input)
  .sort(function (a, b) { return a[0] - b[0] })
  .map(function (i) { return i[1] });

console.log(output);
samanime
  • 25,408
  • 15
  • 90
  • 139
0

Assuming that this object is something you have already and your question is containing a printout of that object.

for(var i in json_data)
   result.push([i, json_data [i]]);

check out this question for a more through answer.

0

Assuming that you object is an ecommerce transaction (but could be cart, or any other) object from dataLayer, and that you'd like to convert it to an array.

  1. set ecommerce object as DL variable

DL variable: ecommerce.purchase object

so it would look like this: GTM debugger: ecommerce object

2] convert it to an array using Custom JS variable:

function(){
  var a = JSON.stringify({{DL EE purchase array}});
  return a;
}

That would convert ecommerce.purchase from object into an array [string data type].

Cacan
  • 1
  • 1