Nikhil Savaliya
Nikhil Savaliya

Reputation: 2166

Generate specific object from array of object

I am having an array like,

var result = [
  {
    "sItem" : [
      "Pizza Margherita","Pizza marinara"
    ],
    "sImage" : [
   "https://assets.marthastewart.com/styles/wmax-300/d31/pizza-margherita-0606-mla102155/pizza-margherita-0606-mla102155_vert.jpg","https://www.silviocicchi.com/pizzachef/wp-content/uploads/2015/02/m-evid-672x372.jpg"
    ],
    "nQuantity" : 1,
    "eDeliveryStatus" : "n",
    "nPrice" : 215,
    "sReceiptId" : "pRjZpGzIDPpX",
  }
];

wants to make Object like, I am running a loop through title array pushing data.

[  
   {  
      "title":"Pizza Margherita",
      "subtitle":"Pizza Margherita",
      "quantity":1,
      "price":215,
      "currency":"INR",
      "image_url":"https://images.mcdelivery.co.in/hardcastle-restaurants-pvt-ltd/image/upload/q_auto:low,fl_lossy,w_300/v1484907263/Items/2754.png"
   },
   {  
      "title":"Pizza marinara",
      "subtitle":"Pizza marinara",
      "quantity":1,
      "price":215,
      "currency":"INR",
      "image_url":"https://www.silviocicchi.com/pizzachef/wp-content/uploads/2015/02/m-evid-672x372.jpg"
   }
]

and this is how i am trying but failing :(,

result.forEach(el => {
  el.sItem.forEach(el2 => {
      elementRec.push({
          "title": el2,
          "subtitle": el2,
          "quantity": el.nQuantity,
          "price": el.nPrice,
          "currency": "INR",
          "image_url": el.sImage
      })
  });
})

I know this is wrong but new to Javascript.

Upvotes: 2

Views: 77

Answers (3)

Nina Scholz
Nina Scholz

Reputation: 386520

You could map the inner sItem and corresponding sImage to new object by some destruction and short properties.

var data = [{ sItem: ["Pizza Margherita", "Pizza marinara"], sImage: ["https://assets.marthastewart.com/styles/wmax-300/d31/pizza-margherita-0606-mla102155/pizza-margherita-0606-mla102155_vert.jpg", "https://www.silviocicchi.com/pizzachef/wp-content/uploads/2015/02/m-evid-672x372.jpg"], nQuantity: 1, eDeliveryStatus: "n", nPrice: 215, sReceiptId: "pRjZpGzIDPpX" }],
    result = data.reduce((r, { sItem, sImage, nQuantity: quantity, nPrice: price }) =>
        r.concat(sItem.map((title, i) => ({
            title, subTitle: title, quantity, price, currency: 'INR', image_url: sImage[i]
        }))),
        []
    );

console.log(result);

Upvotes: 1

mankowitz
mankowitz

Reputation: 2031

I think you are trying to do something like this. First create a new array. I'll call it converted. Then push the result objects into it with .forEach() like this.

var converted = [];
result.forEach(function(i){
    converted.push({

  "title": i.sItem[0],
  "subtitle": i.sItem[0],
  "quantity": i.nQuantity,
  "price": i.nPrice,
  "currency": "INR",
  "image_url": i.sImage[0]

  });
})

Try this fiddle

Upvotes: -1

Guillaume Georges
Guillaume Georges

Reputation: 4020

You are almost there.

In you forEach, add an index parameter and use it to retrieve the right image from the sImage array :

el.sItem.forEach((el2, index) => {
      elementRec.push({
          "title": el2,
          "subtitle": el2,
          "quantity": el.nQuantity,
          "price": el.nPrice,
          "currency": "INR",
          "image_url": el.sImage[index]
      })
  });

var result = [
  {
    "sItem" : [
      "Pizza Margherita",
      "Pizza marinara"
    ],
    "sImage" : [
      "https://assets.marthastewart.com/styles/wmax-300/d31/pizza-margherita-0606-mla102155/pizza-margherita-0606-mla102155_vert.jpg",
      "https://www.silviocicchi.com/pizzachef/wp-content/uploads/2015/02/m-evid-672x372.jpg"
    ],
    "nQuantity" : 1,
    "eDeliveryStatus" : "n",
    "nPrice" : 215,
    "sReceiptId" : "pRjZpGzIDPpX",
  }
];

var elementRec = [];

result.forEach(el => {
  el.sItem.forEach((el2, index) => {
      elementRec.push({
          "title": el2,
          "subtitle": el2,
          "quantity": el.nQuantity,
          "price": el.nPrice,
          "currency": "INR",
          "image_url": el.sImage[index]
      })
  });
});

console.log(elementRec);

Upvotes: 2

Related Questions