我正在尝试从给定的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' }
]发布于 2019-05-26 14:14:21
您将从内部循环返回一个数组,而不是需要创建一个对象来获得所需的结果,
let data = [[{"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"]}]];
let final = data.map(value =>
value.reduce((op, {title, answer}) => {
op[title] = answer
return op
},{})
)
console.log(final)
发布于 2019-05-26 14:13:47
您可以使用.map()和.reduce()方法来获得所需的输出:
const data = [[
{"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"]}
]];
const result = data.map(
arr => arr.reduce((r, {title: k, answer: v}) => (r[k] = v, r), {})
);
console.log(result);.as-console-wrapper { max-height: 100% !important; top: 0; }
发布于 2019-05-26 14:14:03
您应该对子数组使用reduce,以便为每个子数组获取一个对象;
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"]}]];
const res = x.map(e => e.reduce((acc, { title, answer }) => ({ ...acc, [title]: answer }), {}));
console.log(res);
ES5语法:
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"]}]];
var res = x.map(function(e) {
return e.reduce(function(acc, curr) {
return Object.assign({}, acc, { [curr.title]: curr.answer });
}, {});
});
console.log(res);
https://stackoverflow.com/questions/56311101
复制相似问题