简体   繁体   中英

How do I convert this javascript object into an array using javascript without arrow functions?

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',
  }
}

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.

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

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].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM