0

I have one javascript object. I want values like item_name, item_cost etc. from the object. How should I get it ?

This is my javascript object:

data: Array(3)
 0: {Item_name: "bourban-biscuits", Item_cost: 15, Item_quantity: 1, Total_cost: 15, Time: "3/17/2019, 4:26:05 PM"}
 1: {Item_name: "dark fantasy", Item_cost: 5, Item_quantity: 1, Total_cost: 5, Time: "3/17/2019, 4:26:20 PM"}
 2: {Item_name: ""}
asked Apr 18, 2019 at 2:55
5
  • Do you want to get all item_name from the array of objects? Or just one of them? Commented Apr 18, 2019 at 2:56
  • I want all @wentjun Commented Apr 18, 2019 at 2:57
  • You don't have one object, you have an array of objects. And a weird syntax... or is it just me? Commented Apr 18, 2019 at 3:02
  • @RhugvedaDesai Ok.. and what is your desired output? How would you want it to be like Commented Apr 18, 2019 at 3:02
  • i want it in array format [item_name:biscuits, item_cost:5 ] @wentjun Commented Apr 18, 2019 at 3:04

2 Answers 2

1

If you want to get the array of only Item_names, you can use JavaScript's Array.map() operator:

const allNames = data.map(item => item.Item_name}

You can try running this to get a better idea:

const data = [{Item_name: "bourban-biscuits", Item_cost: 15, Item_quantity: 1, Total_cost: 15, Time: "3/17/2019, 4:26:05 PM"}, {Item_name: "dark fantasy", Item_cost: 5, Item_quantity: 1, Total_cost: 5, Time: "3/17/2019, 4:26:20 PM"}, {Item_name: ""}];
 const res = data.map(item => item.Item_name);
 console.log(res);

answered Apr 18, 2019 at 2:58

9 Comments

@RhugvedaDesai I don't really understand what do you want. Can you update the input, and expected output on your question?
And what exactly do you want from data[0]?
i want { Item_name: "bourban-biscuits", Item_cost: 15, Item_quantity: 1, Total_cost: 15, Time: "3/17/2019, 4:26:05 PM"} @wentjun
Isn't that just data[0]? If you want to get that object, why not just use data[0]?
data[0] is undefined @wentjun
|
0

Use map:

const names = array.map(({ Item_name }) => Item_name);
answered Apr 18, 2019 at 3:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.