-2

Can someone please help me with some code to access a key of an object within array. So I have an array of products and within it there's a status.
What I wanna do is write an if statement for each value of status e.g.

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

and so on...

Here is my array:

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"
  }
]
lumio
  • 7,428
  • 4
  • 40
  • 56
joe
  • 59
  • 7

2 Answers2

0

Use 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");
   }
});
riddler
  • 467
  • 3
  • 13
-1
for(i in products)
{
    if(products[i].status == "Refund")
    {
        console.log("Refund item"); 
    }
}
ZiTAL
  • 3,466
  • 8
  • 35
  • 50