【发布时间】:2019-05-26 06:08:54
【问题描述】:
我正在尝试从给定的 json 制作 json 格式
我在 nodejs 中使用 map 函数,但它不能正常工作。我在这里提供所有细节。我想要一个代码,它会给我所需的 json 格式。
给定 Json:
var x =
[
[
{
"title":"My feel about shape up",
"answer":"neutral",
"objectives":[
"Awareness"
]
},
{
"title":"How good is shape up ?",
"answer":"a",
"objectives":[
"Awareness"
]
}
],
[
{
"title":"My feel about shape up",
"answer":"neutral",
"objectives":[
"Awareness"
]
},
{
"title":"How good is shape up ?",
"answer":"Awareness",
"objectives":[
"Awareness"
]
}
]
];
我试过的代码:
result = x.map(function(subarray) {
var data = subarray.map(v =>{
const wd= {[v.title ]: v.answer}
return wd;
})
return data;
})
实际输出:
[ [ { 'My feel about shape up': 'neutral' },
{ 'How good is shape up ?': 'a' } ],
[ { 'My feel about shape up': 'neutral' },
{ 'How good is shape up ?': 'Awareness' } ] ]
预期输出:
[
{ 'My feel about shape up': 'neutral',
'How good is shape up ?': 'a' } ,
{ 'My feel about shape up': 'neutral',
'How good is shape up ?': 'Awareness' }
]
【问题讨论】:
标签: javascript arrays json object