格式化JSON数据

时间:2018-06-06 14:03:00

标签: javascript json

我有以下JSON:

[{"val":5,"sCategory":"1;#Translation Incomplete","CreatedDate":"2018-5-9"},
{"val":2,"sCategory":"2;#VED incorrect/Missing","CreatedDate":"2018-5-11"},
{"val":8,"sCategory":"1;#Translation Incomplete","CreatedDate":"2018-5-11"},
{"val":1,"sCategory":"2;#VED incorrect/Missing","CreatedDate":"2018-5-9"},
{"val":2,"sCategory":"2;#VED incorrect/Missing","CreatedDate":"2018-5-15"},
{"val":1,"sCategory":"1;#Translation Incomplete","CreatedDate":"2018-5-16"},
{"val":1,"sCategory":"12;#Cant get Access","CreatedDate":"2018-6-5"},
{"val":1,"sCategory":"1;#Translation Incomplete","CreatedDate":"2018-5-23"},
{"val":2,"sCategory":"1;#Translation Incomplete","CreatedDate":"2018-5-18"}]

我希望有以下格式才能动态显示:

[{"1;#Translation Incomplete": 5,"CreatedDate":"2018-5-9"},
{"2;#VED incorrect/Missing": 2,"CreatedDate":"2018-5-11"},
{"1;#Translation Incomplete": 8,"CreatedDate":"2018-5-11"},
{"2;#VED incorrect/Missing": 1,"CreatedDate":"2018-5-9"},
{"2;#VED incorrect/Missing": 2,"CreatedDate":"2018-5-15"},
{"1;#Translation Incomplete": 1,"CreatedDate":"2018-5-16"},
{"12;#Cant get Access": 1,"CreatedDate":"2018-6-5"},
{"1;#Translation Incomplete": 1,"CreatedDate":"2018-5-23"},
{"1;#Translation Incomplete": 2,"CreatedDate":"2018-5-18"}]

基本上我想基于sCategory字段动态分组。 我怎样才能用JavaScript实现这个目标?

4 个答案:

答案 0 :(得分:1)

您可以使用地图:

let input = [{"val":5,"sCategory":"1;#Translation Incomplete","CreatedDate":"2018-5-9"},{"val":2,"sCategory":"2;#VED incorrect/Missing","CreatedDate":"2018-5-11"},{"val":8,"sCategory":"1;#Translation Incomplete","CreatedDate":"2018-5-11"},{"val":1,"sCategory":"2;#VED incorrect/Missing","CreatedDate":"2018-5-9"},{"val":2,"sCategory":"2;#VED incorrect/Missing","CreatedDate":"2018-5-15"},{"val":1,"sCategory":"1;#Translation Incomplete","CreatedDate":"2018-5-16"},{"val":1,"sCategory":"12;#Cant get Access","CreatedDate":"2018-6-5"},{"val":1,"sCategory":"1;#Translation Incomplete","CreatedDate":"2018-5-23"},{"val":2,"sCategory":"1;#Translation Incomplete","CreatedDate":"2018-5-18"}];

console.log(input.map(d => { r={}; r[d.sCategory] = d.val; r["CreatedDate"] = d.CreatedDate; return r}));

答案 1 :(得分:1)

请使用map()循环遍历现有数组并返回修改后的结果数组。 在每次迭代中,我们获取sCategory值并将其设为key并为其分配值i =存在于val键。使用CreatedDate我们不需要做任何事情......只需分配已存在的内容即可。

let originalArr = [{"val":5,"sCategory":"1;#Translation Incomplete","CreatedDate":"2018-5-9"},{"val":2,"sCategory":"2;#VED incorrect/Missing","CreatedDate":"2018-5-11"},{"val":8,"sCategory":"1;#Translation Incomplete","CreatedDate":"2018-5-11"},{"val":1,"sCategory":"2;#VED incorrect/Missing","CreatedDate":"2018-5-9"},{"val":2,"sCategory":"2;#VED incorrect/Missing","CreatedDate":"2018-5-15"},{"val":1,"sCategory":"1;#Translation Incomplete","CreatedDate":"2018-5-16"},{"val":1,"sCategory":"12;#Cant get Access","CreatedDate":"2018-6-5"},{"val":1,"sCategory":"1;#Translation Incomplete","CreatedDate":"2018-5-23"},{"val":2,"sCategory":"1;#Translation Incomplete","CreatedDate":"2018-5-18"}];

let newArr = originalArr.map((eachElem) => {
  return {[eachElem.sCategory]: eachElem.val, "CreatedDate": eachElem.CreatedDate}
})

console.log(newArr);

答案 2 :(得分:1)

将数组用作编写是不是更容易,因此您确定每条记录都具有val,cCategory和CreatedDate属性,并且只更新创建显示的循环?

动态对象属性很难处理,一旦你必须按值排序或按sCategory排序。

const input = [{"val":5,"sCategory":"1;#Translation Incomplete","CreatedDate":"2018-5-9"},{"val":2,"sCategory":"2;#VED incorrect/Missing","CreatedDate":"2018-5-11"},{"val":8,"sCategory":"1;#Translation Incomplete","CreatedDate":"2018-5-11"},{"val":1,"sCategory":"2;#VED incorrect/Missing","CreatedDate":"2018-5-9"},{"val":2,"sCategory":"2;#VED incorrect/Missing","CreatedDate":"2018-5-15"},{"val":1,"sCategory":"1;#Translation Incomplete","CreatedDate":"2018-5-16"},{"val":1,"sCategory":"12;#Cant get Access","CreatedDate":"2018-6-5"},{"val":1,"sCategory":"1;#Translation Incomplete","CreatedDate":"2018-5-23"},{"val":2,"sCategory":"1;#Translation Incomplete","CreatedDate":"2018-5-18"}];
const output = input.map( entry => ( { [ entry.sCategory ] : entry.val, "CreatedDate": entry.CreatedDate } ) );
console.log( output );

答案 3 :(得分:0)

在JavaScript中使用对象很容易。遍历数组并使用所需格式创建新对象,然后将其添加到新数组中。您也可以使用map()函数,但下面的简单代码将在所有浏览器中运行:



var json = [{
  "val": 5,
  "sCategory": "1;#Translation Incomplete",
  "CreatedDate": "2018-5-9"
}, {
  "val": 2,
  "sCategory": "2;#VED incorrect/Missing",
  "CreatedDate": "2018-5-11"
}, {
  "val": 8,
  "sCategory": "1;#Translation Incomplete",
  "CreatedDate": "2018-5-11"
}, {
  "val": 1,
  "sCategory": "2;#VED incorrect/Missing",
  "CreatedDate": "2018-5-9"
}, {
  "val": 2,
  "sCategory": "2;#VED incorrect/Missing",
  "CreatedDate": "2018-5-15"
}, {
  "val": 1,
  "sCategory": "1;#Translation Incomplete",
  "CreatedDate": "2018-5-16"
}, {
  "val": 1,
  "sCategory": "12;#Cant get Access",
  "CreatedDate": "2018-6-5"
}, {
  "val": 1,
  "sCategory": "1;#Translation Incomplete",
  "CreatedDate": "2018-5-23"
}, {
  "val": 2,
  "sCategory": "1;#Translation Incomplete",
  "CreatedDate": "2018-5-18"
}];

var json2 = [];
for (var i = 0; i < json.length; i++) {
  json2.push({
    [json[i].sCategory]: json[i].val,
    "CreatedDate": json[i].CreatedDate
  });
}
//The line below is for demo only.
console.log(json2);
&#13;
&#13;
&#13;