2

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.

7
  • 2
    "image_url": el.sImage to "image_url": el.sImage[0] maybe Commented Jun 25, 2018 at 9:09
  • images are like a title a dynamic array Commented Jun 25, 2018 at 9:10
  • please add the wanted result as well. Commented Jun 25, 2018 at 9:12
  • 1
    How are you failing, as Efe mentioned you should use el.sImage[0] since you have sImage attribute as an array Commented Jun 25, 2018 at 9:15
  • 1
    please with data, not abstract. where do you get the quantity from, where does the second image go to, etc. pp. Commented Jun 25, 2018 at 9:15

3 Answers 3

2

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

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

1 Comment

both Image are same here
1

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

Comments

-1

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

Comments

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.