Rhugveda Desai
Rhugveda Desai

Reputation: 84

How to get values from javascript object?

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

Upvotes: 0

Views: 67

Answers (2)

Jack Bashford
Jack Bashford

Reputation: 44087

Use map:

const names = array.map(({ Item_name }) => Item_name);

Upvotes: 0

wentjun
wentjun

Reputation: 42516

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

Upvotes: 1

Related Questions