如何访问对象内的项目

时间:2018-11-05 15:08:28

标签: javascript arrays

有人可以帮我一些代码来访问数组中对象的键吗?所以我有一系列产品,并且里面有一个状态。
我想做的是为每个状态值编写一个if语句,例如

if (prod.status == "Refund") { console.log("Refund item"); }

以此类推...

这是我的数组:

var products = [
  {
    "order_id": "241918",
    "product_id": "152737",
    "order_qty": "1",
    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
    "out_of_stock": "find_best_match",
    "status": "Complete",
    "id": "282",
    "vendor": "Pick n Pay",
    "price": "40.69"
  },
  {
    "order_id": "241918",
    "product_id": "155565",
    "order_qty": "3",
    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
    "out_of_stock": "find_best_match",
    "status": "Complete",
    "id": "283",
    "vendor": "Pick n Pay",
    "price": "65.99"
  },
  {
    "order_id": "241918",
    "product_id": "153895",
    "order_qty": "2",
    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
    "out_of_stock": "find_best_match",
    "status": "Complete",
    "id": "284",
    "vendor": "Pick n Pay",
    "price": "26.39"
  },
  {
    "order_id": "241918",
    "product_id": "148155",
    "order_qty": "1",
    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
    "out_of_stock": "find_best_match",
    "status": "Complete",
    "id": "285",
    "vendor": "Pick n Pay",
    "price": "34.86"
  },
  {
    "order_id": "241918",
    "product_id": "137556",
    "order_qty": "8",
    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
    "out_of_stock": "find_best_match",
    "status": "Complete",
    "id": "286",
    "vendor": "Pick n Pay",
    "price": "6.59"
  },
  {
    "order_id": "241918",
    "product_id": "153523",
    "order_qty": "1",
    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
    "out_of_stock": "find_best_match",
    "status": "Complete",
    "id": "287",
    "vendor": "Pick n Pay",
    "price": "40.69"
  },
  {
    "order_id": "241918",
    "product_id": "154262",
    "order_qty": "1",
    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
    "out_of_stock": "find_best_match",
    "status": "Complete",
    "id": "288",
    "vendor": "Pick n Pay",
    "price": "25.29"
  },
  {
    "order_id": "241918",
    "product_id": "151486",
    "order_qty": "1",
    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
    "out_of_stock": "find_best_match",
    "status": "Complete",
    "id": "289",
    "vendor": "Pick n Pay",
    "price": "72.59"
  },
  {
    "order_id": "241918",
    "product_id": "137924",
    "order_qty": "1",
    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
    "out_of_stock": "find_best_match",
    "status": "Complete",
    "id": "290",
    "vendor": "Pick n Pay",
    "price": "128.63"
  },
  {
    "order_id": "241918",
    "product_id": "158980",
    "order_qty": "1",
    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
    "out_of_stock": "find_best_match",
    "status": "Complete",
    "id": "291",
    "vendor": "Pick n Pay",
    "price": "189.19"
  },
  {
    "order_id": "241918",
    "product_id": "158650",
    "order_qty": "8",
    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
    "out_of_stock": "find_best_match",
    "status": "Complete",
    "id": "292",
    "vendor": "Pick n Pay",
    "price": "7.69"
  }
]

2 个答案:

答案 0 :(得分:0)

使用forEach()

var products = [
  {
    "order_id": "241918",
    "product_id": "152737",
    "order_qty": "1",
    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
    "out_of_stock": "find_best_match",
    "status": "Complete",
    "id": "282",
    "vendor": "Pick n Pay",
    "price": "40.69"
  },
  {
    "order_id": "241918",
    "product_id": "155565",
    "order_qty": "3",
    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
    "out_of_stock": "find_best_match",
    "status": "Complete",
    "id": "283",
    "vendor": "Pick n Pay",
    "price": "65.99"
  },
  {
    "order_id": "241918",
    "product_id": "153895",
    "order_qty": "2",
    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
    "out_of_stock": "find_best_match",
    "status": "Complete",
    "id": "284",
    "vendor": "Pick n Pay",
    "price": "26.39"
  },
  {
    "order_id": "241918",
    "product_id": "148155",
    "order_qty": "1",
    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
    "out_of_stock": "find_best_match",
    "status": "Complete",
    "id": "285",
    "vendor": "Pick n Pay",
    "price": "34.86"
  },
  {
    "order_id": "241918",
    "product_id": "137556",
    "order_qty": "8",
    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
    "out_of_stock": "find_best_match",
    "status": "Complete",
    "id": "286",
    "vendor": "Pick n Pay",
    "price": "6.59"
  },
  {
    "order_id": "241918",
    "product_id": "153523",
    "order_qty": "1",
    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
    "out_of_stock": "find_best_match",
    "status": "Complete",
    "id": "287",
    "vendor": "Pick n Pay",
    "price": "40.69"
  },
  {
    "order_id": "241918",
    "product_id": "154262",
    "order_qty": "1",
    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
    "out_of_stock": "find_best_match",
    "status": "Complete",
    "id": "288",
    "vendor": "Pick n Pay",
    "price": "25.29"
  },
  {
    "order_id": "241918",
    "product_id": "151486",
    "order_qty": "1",
    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
    "out_of_stock": "find_best_match",
    "status": "Complete",
    "id": "289",
    "vendor": "Pick n Pay",
    "price": "72.59"
  },
  {
    "order_id": "241918",
    "product_id": "137924",
    "order_qty": "1",
    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
    "out_of_stock": "find_best_match",
    "status": "Complete",
    "id": "290",
    "vendor": "Pick n Pay",
    "price": "128.63"
  },
  {
    "order_id": "241918",
    "product_id": "158980",
    "order_qty": "1",
    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
    "out_of_stock": "find_best_match",
    "status": "Complete",
    "id": "291",
    "vendor": "Pick n Pay",
    "price": "189.19"
  },
  {
    "order_id": "241918",
    "product_id": "158650",
    "order_qty": "8",
    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
    "out_of_stock": "find_best_match",
    "status": "Complete",
    "id": "292",
    "vendor": "Pick n Pay",
    "price": "7.69"
  }
];

products.forEach(function(product) {
  console.log(product);
  if (product.status == "Refund") {
    console.log("Refund item");
   }
});

答案 1 :(得分:-1)

for(i in products)
{
    if(products[i].status == "Refund")
    {
        console.log("Refund item"); 
    }
}