根据项目要求将json对象数组解析为json数组的特定格式

时间:2019-04-17 12:55:27

标签: javascript json

基于我在项目中的requiremrnt,我将以下json数组作为输入,但是我需要将其转换为json对象数组的特定格式。

[
  {
    "accident_description": "bike accident",
    "reported_by": "john",

  },
   {
    "accident_description": "car accident",
    "reported_by": "sam",

  }
]

输出>>>

  "fields": [
    {
      "title": "accident_description",
      "values": "bike accident"
      "type": "generic",

    },
    {
      "title": "reported_by",
      "values": "john",
      "type": "generic",

    },
    {
      "title": "accident_description",
      "values": "car accident"
      "type": "generic",

    },
    {
      "title": "reported_by",
      "values": "sam",
      "type": "generic",

    },
  ]

5 个答案:

答案 0 :(得分:2)

您可以像这样map Object.entires每个对象:

const input=[{"accident_description":"bike accident","reported_by":"john",},{"accident_description":"car accident","reported_by":"sam",}],
      type = "generic";

const output = input.flatMap(Object.entries)
                    .map(([title, values]) => ({ title, values, type }))
     
console.log(output)

答案 1 :(得分:2)

您可以使用reduce函数获取所需的输出:-

[
  {
    "accident_description": "bike accident",
    "reported_by": "john",

  },
   {
    "accident_description": "car accident",
    "reported_by": "sam",

  }
].reduce((arr,item) => {
    for(let key in item){
        arr.push({
            "title" : key,
            "values" : item[key],
            "type"  : "generic"
        });
    }
    return arr;

},[]);

答案 2 :(得分:1)

您可以使用flatMap()map()

  • flatMap()内使用Object.entires()获取对象的条目
  • 然后将map()应用于条目并返回一个对象。

const arr = [ { "accident_description": "bike accident", "reported_by": "john", }, { "accident_description": "car accident", "reported_by": "sam", } ];

let res = arr.flatMap(x => (Object.entries(x).map(([k,v]) => ({title:k,values:v,type:"generic"}))));

console.log(res)

答案 3 :(得分:1)

您可以使用flatMap遍历数组。使用Object.entries将每个对象转换为数组,然后使用map返回所需的对象。

let arr = [{"accident_description":"bike accident","reported_by":"john"},{"accident_description":"car accident","reported_by":"sam"}];

let result = arr.flatMap(o => Object.entries(o).map(([v, k]) => ({"title": v,"values": k,"type": "generic"})))

console.log(result);

...或者您也可以使用concat代替flatMap

let arr = [{"accident_description":"bike accident","reported_by":"john"},{"accident_description":"car accident","reported_by":"sam"}];

let result = [].concat(...arr.flatMap(o => Object.entries(o).map(([v, k]) => ({"title": v,"values": k,"type": "generic"}))));

console.log(result);

答案 4 :(得分:-1)

您可以使用flatMapreduce来完成您想做的事情

flatMap而非map将通过循环输入来创建平面数组。也就是说,您输入数组中的第一个obj将成为输出数组中的两个obj。因此,将两个对象合并到一个数组flatMap中将使它变得平坦,即两个单独的条目。然后使用reduce和concat格式化一个obj的数据。

const input = [{
    "accident_description": "bike accident",
    "reported_by": "john",

  },
  {
    "accident_description": "car accident",
    "reported_by": "sam",

  }
]

const out = input.flatMap(obj =>
  Object.keys(obj)
  .reduce((acc, cur) =>
    acc.concat({
      title: cur,
      values: obj[cur],
      type: "dynamic"
    }), [])
)

console.log(out)