我正在做一个项目来列出乘客的详细信息,所以我将乘客对象列表存储在一个数组中,如下所示:
var object = {
passengerDetails[0].age: "22"
,
passengerDetails[0].gender: "M"
,
passengerDetails[0].name: "asdfasdf"
passengerDetails[1].age: "23"
,
passengerDetails[1].gender: "F"
,
passengerDetails[1].name: "xxx"
}
但我需要的是:
var object = {
"passengerDetails" : [{"age":"22", "gender" :"M" ,"name" : "asdfasdf"},
{"age":"23", "gender" :"F" ,"name" : "xxx"}] }
上述转换有什么办法吗?也欢迎替代方式。提前致谢
答案 0 :(得分:0)
如果您将第一个:
更改为=
(我假设passengerDetails
是一个变量),那么您的第二个示例是正确的,并且理想情况下添加{{1}最后:
;

var passengerDetails = [
{
"age": "22",
"gender": "M",
"name": "asdfasdf"
},
{
"age": "23",
"gender": "F",
"name": "xxx"
}
];
snippet.log(passengerDetails[0].age); // 22