Javascript 从对象数组生成特定对象
我有一个数组Javascript 从对象数组生成特定对象,javascript,arrays,json,object,Javascript,Arrays,Json,Object,我有一个数组 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://ww
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",
}
];
要使对象类似,我正在运行一个循环,通过标题数组推送数据
[
{
"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"
}
]
这就是我努力但失败的方式:(
我知道这是错误的,但对Javascript来说是新的。您就快到了
在forEach
中,添加一个index
参数,并使用它从sImage
数组中检索右图像:
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结果=[
{
“sItem”:[
“玛格丽塔披萨”,
“比萨玛丽娜”
],
“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"
],
"质询":一,,
“eDeliveryStatus”:“n”,
“nPrice”:215,
“sReceiptId”:“pRjZpGzIDPpX”,
}
];
var elementRec=[];
result.forEach(el=>{
el.sItem.forEach((el2,索引)=>{
元素记录推送({
“标题”:el2,
“副标题”:el2,
“数量”:el.nQuantity,
“价格”:el.nPrice,
“货币”:“印度卢比”,
“图像url”:el.sImage[索引]
})
});
});
console.log(elementRec);
我想您正在尝试这样做。首先创建一个新数组。我将其称为已转换的
。然后像这样使用.forEach()
将结果
对象推入其中
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]
});
})
试试这个你可以通过一些破坏和短属性将内部的
站点m
和相应的sImage
映射到新对象
var data=[{sItem:[“玛格丽塔比萨饼”、“玛丽娜比萨饼”],西玛吉:[”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“],质询:1,eDeliveryStatus:“n”,nPrice:215,sReceiptId:“pRjZpGzIDPpX”},
结果=数据。减少((r,{sItem,sImage,nQuantity:quantity,nPrice:price})=>
r、 concat(sItem.map)(标题,i)=>({
标题,副标题:标题,数量,价格,货币:'INR',图片地址:sImage[i]
}))),
[]
);
console.log(result);
“image\u url”:el.sImage
到“image\u url”:el.sImage[0]
可能图像就像一个标题,一个动态数组,请添加想要的结果。您怎么会失败,正如您提到的,您应该使用el.sImage[0]
因为您将sImage属性作为带有数据的数组租约,而不是抽象的。您从哪里获取数量,第二张图像到哪里,等等。pp。这两张图像在这里是相同的
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]
});
})