1

My question is probably simple but my eyes is blinking and I cannot think something else right now. Any help appreciated

This is my initial array

var Products = [
   {
     "order_product_id":"1111", 
     "order_id":"555", 
     "product_id":"111", 
     "name":"name1", 
     "model":"111", 
     "quantity":"1", 
     "price":"100", 
     "total":"100", 
     "tax":"20",
   },
   {      
     "order_product_id":"2222",    
     "order_id":"555",      
     "product_id":"222",      
     "name":"name2",      
     "model":"222",      
     "quantity":"1",      
     "price":"200",      
     "total":"200",      
     "tax":"40",      

   }
];

and this is the array that i need (change names and exclude some elements)

{
      "id": "1111",
      "name": "name1",
      "category": "",
      "list_position": 1,
      "quantity": 1,
      "price": '100'
    },
    {
      "id": "2222",
      "name": "name2",
      "category": " ",
      "list_position": 2,
      "quantity": 1,
      "price": '200'
    }

finally this is where i want to put the new array of products after manipulation

   {
    "transaction_id": 1,
    "value": 99,
    "currency": "EUR",
    "shipping": 0,
    "items": Items
   }

I tried push / forEach / even string but I cannot manage to have a final clean array as Google wants like this

gtag('event', 'purchase', {
  "transaction_id": 1,
  "value": 99,
  "currency": "EUR",
  "shipping": 0,
  "items": [
    {
      "id": "1111",
      "name": "name1",
      "category": "",
      "list_position": 1,
      "quantity": 1,
      "price": '100'
    },
    {
      "id": "2222",
      "name": "name2",
      "category": " ",
      "list_position": 2,
      "quantity": 1,
      "price": '200'
    }
  ]
});
2
  • It'd be nice to see code that you tried. As-is, you haven't shared your attempts. We could give MUCH better feedback / answers if you'd include your attempted code (even though it didn't work) Commented Oct 17, 2019 at 15:58
  • I knew that the correct answer was somewhere between map and each but i didn't want to affect the thinking procedure. Sometimes we micro-correcting posted codes but missing the final goal. On the other hand I was not 100% sure for my procedure Commented Oct 18, 2019 at 7:35

2 Answers 2

2

You can create your own method to build your object and use Array.prototype.map to iterate through.

var Products = [
  {
    order_product_id: "1111",
    order_id: "555",
    product_id: "111",
    name: "name1",
    model: "111",
    quantity: "1",
    price: "100",
    total: "100",
    tax: "20"
  },
  {
    order_product_id: "2222",
    order_id: "555",
    product_id: "222",
    name: "name2",
    model: "222",
    quantity: "1",
    price: "200",
    total: "200",
    tax: "40"
  }
];

let getObj = (obj, i) => ({
  id: obj.order_product_id,
  name: obj.name,
  category: obj.category || "",
  list_position: i + 1,
  quantity: obj.quantity,
  price: obj.price
});

let Items = Products.map(getObj);
console.log(Items);

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much exactly what I needed
1

I've looped through the product list and created an item for each one to push it to the items' list with the new format.

Here you go:

var products = [{
    "order_product_id": "1111",
    "order_id": "555",
    "product_id": "111",
    "name": "name1",
    "model": "111",
    "quantity": "1",
    "price": "100",
    "total": "100",
    "tax": "20",
  },
  {
    "order_product_id": "2222",
    "order_id": "555",
    "product_id": "222",
    "name": "name2",
    "model": "222",
    "quantity": "1",
    "price": "200",
    "total": "200",
    "tax": "40",

  }
];

let destinationObject = {
  "transaction_id": 1,
  "value": 99,
  "currency": "EUR",
  "shipping": 0,
  "items": []
};


products.forEach(function(element, index) {

  let item = {
    "id": element.order_product_id,
    "name": element.name,
    "category": "",
    "list_position": index,
    "quantity": element.quantity,
    "price": element.price
  };

  destinationObject.items.push(item);

});

console.log('destinationObject', destinationObject);

2 Comments

@cale_b You're right, I've added a description on how I've done it.
Thank you very much! Most complete answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.