-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: ' ' } 
1
  • req.body looks like an object and not an array Commented Aug 22, 2019 at 10:36

2 Answers 2

-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]); })

Sign up to request clarification or add additional context in comments.

3 Comments

This question should be closed ,its a duplicate
Of course. That is why I marked it as a duplicate.
Any reason for the down vote?
-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]); } }

5 Comments

Why that downvotes?!!? Don't know if someone is doing it on purpose or so, but it's not funny ... It's the correct answer and I don't get what is wrong ...
Thanks.....I need to take quantity_1,item_name_1, amount_1 only means what i need to do?
I'll edit it, give me a second
@DvThiyanesh I added one if statement inside for loop to show only your required values
Not exact one if there are 20 quantity and their name and price then to fetch it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.