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: ""}
Rhugveda DesaiRhugveda Desai
asked Apr 18, 2019 at 2:55
2 Answers 2
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
wentjun
@RhugvedaDesai I don't really understand what do you want. Can you update the input, and expected output on your question?
wentjun
And what exactly do you want from data[0]?
Rhugveda Desai
i want { Item_name: "bourban-biscuits", Item_cost: 15, Item_quantity: 1, Total_cost: 15, Time: "3/17/2019, 4:26:05 PM"} @wentjun
wentjun
Isn't that just data[0]? If you want to get that object, why not just use
data[0]
?Rhugveda Desai
data[0] is undefined @wentjun
|
Use map
:
const names = array.map(({ Item_name }) => Item_name);
answered Apr 18, 2019 at 3:07
Comments
lang-js
item_name
from the array of objects? Or just one of them?