【问题标题】:How can i reassemble the following json using map function?如何使用 map 函数重新组装以下 json?
【发布时间】: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


    【解决方案1】:

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

    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)

    【讨论】:

      【解决方案2】:

      您可以使用.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; }

      【讨论】:

        【解决方案3】:

        您应该为子数组使用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);

        【讨论】:

          猜你喜欢
          • 2022-07-07
          • 2021-10-08
          • 2019-12-02
          • 1970-01-01
          • 2018-08-21
          • 1970-01-01
          • 2019-03-11
          • 2022-09-23
          • 2019-02-27
          相关资源
          最近更新 更多