有人可以帮我一些代码,根据ID从数组中获取数据,我有一个包含项目详细信息的数组,我只想从数组中返回所有价格。这是我的数组:`var prod = [
{
"id": "282",
"product_id": "152737",
"sku": "b175a9ea5f4d9b4766e74079c2bec8",
"prod_name": "PnP Baby Marrows 1kg",
"vendor": "Pick n Pay",
"price": "40.69",
"order_qty": "1"
},
{
"id": "283",
"product_id": "155565",
"sku": "414a1c04ce7fe72269e116d3dd95d3",
"prod_name": "PnP UHT Low Fat Milk 1 Litre x6",
"vendor": "Pick n Pay",
"price": "65.99",
"order_qty": "3"
},
{
"id": "284",
"product_id": "153895",
"sku": "769fca303aa745a43412af20c29e48",
"prod_name": "Plush Dark Furniture Oil 300Ml",
"vendor": "Pick n Pay",
"price": "26.39",
"order_qty": "2"
},
{
"id": "285",
"product_id": "148155",
"sku": "2477f9462d50d0e7b40631c1a347b2",
"prod_name": "Pnp Chicken 4 Thighs And 4 Drumsticks",
"vendor": "Pick n Pay",
"price": "34.86",
"order_qty": "1"
}
]
答案 0 :(得分:2)
您应该结合使用map
方法(通过传递callback
提供的函数,该函数应用于给定数组中的每个项目)与 destructuring 结合使用。
let arr = [{ "id": "282", "product_id": "152737", "sku": "b175a9ea5f4d9b4766e74079c2bec8", "prod_name": "PnP Baby Marrows 1kg", "vendor": "Pick n Pay", "price": "40.69", "order_qty": "1" }, { "id": "283", "product_id": "155565", "sku": "414a1c04ce7fe72269e116d3dd95d3", "prod_name": "PnP UHT Low Fat Milk 1 Litre x6", "vendor": "Pick n Pay", "price": "65.99", "order_qty": "3" }, { "id": "284", "product_id": "153895", "sku": "769fca303aa745a43412af20c29e48", "prod_name": "Plush Dark Furniture Oil 300Ml", "vendor": "Pick n Pay", "price": "26.39", "order_qty": "2" }, { "id": "285", "product_id": "148155", "sku": "2477f9462d50d0e7b40631c1a347b2", "prod_name": "Pnp Chicken 4 Thighs And 4 Drumsticks", "vendor": "Pick n Pay", "price": "34.86", "order_qty": "1" } ]
let prices = arr.map(({price}) => price);
console.log(prices);
答案 1 :(得分:0)
使用数组过滤器执行此操作。
var prod = [
{
"id": "282",
"product_id": "152737",
"sku": "b175a9ea5f4d9b4766e74079c2bec8",
"prod_name": "PnP Baby Marrows 1kg",
"vendor": "Pick n Pay",
"price": "40.69",
"order_qty": "1"
},
{
"id": "283",
"product_id": "155565",
"sku": "414a1c04ce7fe72269e116d3dd95d3",
"prod_name": "PnP UHT Low Fat Milk 1 Litre x6",
"vendor": "Pick n Pay",
"price": "65.99",
"order_qty": "3"
},
{
"id": "284",
"product_id": "153895",
"sku": "769fca303aa745a43412af20c29e48",
"prod_name": "Plush Dark Furniture Oil 300Ml",
"vendor": "Pick n Pay",
"price": "26.39",
"order_qty": "2"
},
{
"id": "285",
"product_id": "148155",
"sku": "2477f9462d50d0e7b40631c1a347b2",
"prod_name": "Pnp Chicken 4 Thighs And 4 Drumsticks",
"vendor": "Pick n Pay",
"price": "34.86",
"order_qty": "1"
}
]
const getData = (id) => {
return prod.filter( (f) => { return f.id === id });
}
console.log(getData("284"));
答案 2 :(得分:0)
希望这对您有所帮助。
var prod = [
{
"id": "282",
"product_id": "152737",
"sku": "b175a9ea5f4d9b4766e74079c2bec8",
"prod_name": "PnP Baby Marrows 1kg",
"vendor": "Pick n Pay",
"price": "40.69",
"order_qty": "1"
},
{
"id": "283",
"product_id": "155565",
"sku": "414a1c04ce7fe72269e116d3dd95d3",
"prod_name": "PnP UHT Low Fat Milk 1 Litre x6",
"vendor": "Pick n Pay",
"price": "65.99",
"order_qty": "3"
},
{
"id": "284",
"product_id": "153895",
"sku": "769fca303aa745a43412af20c29e48",
"prod_name": "Plush Dark Furniture Oil 300Ml",
"vendor": "Pick n Pay",
"price": "26.39",
"order_qty": "2"
},
{
"id": "285",
"product_id": "148155",
"sku": "2477f9462d50d0e7b40631c1a347b2",
"prod_name": "Pnp Chicken 4 Thighs And 4 Drumsticks",
"vendor": "Pick n Pay",
"price": "34.86",
"order_qty": "1"
}
]
var a=[];
prod.forEach((item=>{a.push(item.price)}));
console.log(a);
答案 3 :(得分:0)
var prod = [
{
"id": "282",
"product_id": "152737",
"sku": "b175a9ea5f4d9b4766e74079c2bec8",
"prod_name": "PnP Baby Marrows 1kg",
"vendor": "Pick n Pay",
"price": "40.69",
"order_qty": "1"
},
{
"id": "283",
"product_id": "155565",
"sku": "414a1c04ce7fe72269e116d3dd95d3",
"prod_name": "PnP UHT Low Fat Milk 1 Litre x6",
"vendor": "Pick n Pay",
"price": "65.99",
"order_qty": "3"
},
{
"id": "284",
"product_id": "153895",
"sku": "769fca303aa745a43412af20c29e48",
"prod_name": "Plush Dark Furniture Oil 300Ml",
"vendor": "Pick n Pay",
"price": "26.39",
"order_qty": "2"
}
]
let res = prod.map(item => item.price);
console.log(res);
这仅返回价格,但使用map函数将其返回数组。
答案 4 :(得分:0)
您可以使用Array.map来创建一个新数组,可以使用es6语法
请参见下面的代码
var data = [
{
"id": "282",
"product_id": "152737",
"sku": "b175a9ea5f4d9b4766e74079c2bec8",
"prod_name": "PnP Baby Marrows 1kg",
"vendor": "Pick n Pay",
"price": "40.69",
"order_qty": "1"
},
{
"id": "283",
"product_id": "155565",
"sku": "414a1c04ce7fe72269e116d3dd95d3",
"prod_name": "PnP UHT Low Fat Milk 1 Litre x6",
"vendor": "Pick n Pay",
"price": "65.99",
"order_qty": "3"
},
{
"id": "284",
"product_id": "153895",
"sku": "769fca303aa745a43412af20c29e48",
"prod_name": "Plush Dark Furniture Oil 300Ml",
"vendor": "Pick n Pay",
"price": "26.39",
"order_qty": "2"
},
{
"id": "285",
"product_id": "148155",
"sku": "2477f9462d50d0e7b40631c1a347b2",
"prod_name": "Pnp Chicken 4 Thighs And 4 Drumsticks",
"vendor": "Pick n Pay",
"price": "34.86",
"order_qty": "1"
}
]
// es6
var pricesES6 = data.map(o => o.price)
// vanilla js
var pricesVJS = data.map(function(o){
return o.price
})
console.log("prices using es6 =>", pricesES6)
console.log("prices using prices vanilla js ", pricesVJS )