Stavros
Stavros

Reputation: 831

Transform Array of Objects to Array of Different Structured Objects

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'
    }
  ]
});

Upvotes: 1

Views: 53

Answers (2)

arizafar
arizafar

Reputation: 3122

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

Upvotes: 2

Diogo Peres
Diogo Peres

Reputation: 1372

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

Upvotes: 1

Related Questions