如何从javascript对象获取值?

时间:2019-04-18 02:55:14

标签: javascript arrays object

我有一个JavaScript对象。我想要对象中的值itm_nameitem_ost等。我该怎么办?

这是我的javascript对象:

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

2 个答案:

答案 0 :(得分:0)

如果只想获取Item_names的数组,则可以使用JavaScript的Array.map()运算符:

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

您可以尝试运行此命令以获得更好的主意:

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

答案 1 :(得分:0)

使用map

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