我有一个对象数组,比如-
array = [{label:"1",value:"11"},
{label:"2",value:"22"},
{label:"3",value:"33"},
{label:"4",value:"44"},
];
如何获取第一个对象(11)的value
?
发布于 2019-03-02 05:49:54
只需使用array[0].value
即可
var array = [{
label: "1",
value: "11"
},
{
label: "2",
value: "22"
},
{
label: "3",
value: "33"
},
{
label: "4",
value: "44"
},
];
var firstValue = array[0].value;
console.log(firstValue);
您也可以像这样使用解构:
var array = [{
label: "1",
value: "11"
},
{
label: "2",
value: "22"
},
{
label: "3",
value: "33"
},
{
label: "4",
value: "44"
},
];
var [{ value: firstValue }] = array;
console.log(firstValue);
发布于 2019-03-02 05:54:44
您可以通过获取带有键0(仅是数组的第一个元素)的对象,然后获取value
属性来获取一个时髦的destructuring。
var array = [{ label: "1", value: "11" }, { label: "2", value: "22" }, { label: "3", value: "33" }, { label: "4", value: "44" }],
{ 0: { value } } = array;
console.log(value);
发布于 2019-03-02 05:50:40
只需获取array
的0
索引中的项。有关更多详细信息,请参阅评论:
//The initial data
var array = [{
label: "1",
value: "11"
},
{
label: "2",
value: "22"
},
{
label: "3",
value: "33"
},
{
label: "4",
value: "44"
},
];
//Access the item you want by it's index in the array (0-based)
var firstItem = array[0];
//Grab the "value" property from the object
var value = firstItem.value;
//Log your value
console.log(value);
https://stackoverflow.com/questions/54952746
复制相似问题