首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用map函数重新组装下面的json?

如何使用map函数重新组装下面的json?
EN

Stack Overflow用户
提问于 2019-05-26 14:08:54
回答 3查看 35关注 0票数 2

我正在尝试从给定的json生成json格式。

我在nodejs中使用了map函数,但它不能正常工作。我在这里给出所有的细节。我想要一个代码,将给我所需的json格式。

给定Json:

代码语言:javascript
复制
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"
        ]
    }
]
];

我尝试过的代码:

代码语言:javascript
复制
result = x.map(function(subarray) {
var data  = subarray.map(v =>{
  const wd= {[v.title ]: v.answer}
   return wd;
   })
   return data;

})

实际输出:

代码语言:javascript
复制
[ [ { '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
复制
[ 
{ 'My feel about shape up': 'neutral',
'How good is shape up ?': 'a' } ,
{ 'My feel about shape up': 'neutral',
'How good is shape up ?': 'Awareness' } 
]
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-05-26 14:14:21

您将从内部循环返回一个数组,而不是需要创建一个对象来获得所需的结果,

代码语言:javascript
复制
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)

票数 1
EN

Stack Overflow用户

发布于 2019-05-26 14:13:47

您可以使用.map().reduce()方法来获得所需的输出:

代码语言:javascript
复制
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);
代码语言:javascript
复制
.as-console-wrapper { max-height: 100% !important; top: 0; }

票数 1
EN

Stack Overflow用户

发布于 2019-05-26 14:14:03

您应该对子数组使用reduce,以便为每个子数组获取一个对象;

代码语言:javascript
复制
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语法:

代码语言:javascript
复制
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);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56311101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档