-1

I have one array from req.body. I need to loop through and fetch the value like(quantity_1 as one separate data and quantity_2 as separate data) How to fetch like this and My Array from req.body is:

 { quantity_1: '9',
  item_name_1: 'Event Cap Nike',
  amount_1: '599',
  shipping_1: 'undefined',
  shipping2_1: 'undefined',
  quantity_2: '1',
  item_name_2: 'Plaza',
  amount_2: '1000',
  shipping_2: 'undefined',
  shipping2_2: 'undefined',
  cmd: '_cart',
  upload: '1',
  bn: 'MiniCart_AddToCart_WPS_US',
  business: ' ',
  currency_code: 'INR',
  return: ' ',
  cancel_return: ' ' }
Charlie
  • 22,886
  • 11
  • 59
  • 90

2 Answers2

-2

Actually, this is not an array but an object and you can iterate and console.log its values by using for ... in loop like this:

const myObj = { quantity_1: '9',
  item_name_1: 'Event Cap Nike',
  amount_1: '599',
  shipping_1: 'undefined',
  shipping2_1: 'undefined',
  quantity_2: '1',
  item_name_2: 'Plaza',
  amount_2: '1000',
  shipping_2: 'undefined',
  shipping2_2: 'undefined',
  cmd: '_cart',
  upload: '1',
  bn: 'MiniCart_AddToCart_WPS_US',
  business: ' ',
  currency_code: 'INR',
  return: ' ',
  cancel_return: ' ' }

for(let key in myObj) {
    if(key in ["quantity_1", "item_name_1", "amount_1"]) {
        console.log(myObj[key]);
    }
}
Dominik Matis
  • 2,086
  • 10
  • 15
-2
  1. This is not an array. This is an object.

  2. You can use Object.keys method among others to iterate the object properties.

 var myObj = { quantity_1: '9',
  item_name_1: 'Event Cap Nike',
  amount_1: '599',
  shipping_1: 'undefined',
  shipping2_1: 'undefined',
  quantity_2: '1',
  item_name_2: 'Plaza',
  amount_2: '1000',
  shipping_2: 'undefined',
  shipping2_2: 'undefined',
  cmd: '_cart',
  upload: '1',
  bn: 'MiniCart_AddToCart_WPS_US',
  business: ' ',
  currency_code: 'INR',
  return: ' ',
  cancel_return: ' ' }
  
  Object.keys(myObj).forEach(prop => {
  
      console.log(myObj[prop]);
  })
Charlie
  • 22,886
  • 11
  • 59
  • 90