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: ""}
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);

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.