将JSON数据转换为JavaScript 2D数组

时间:2015-10-30 08:25:04

标签: javascript json

我有以下JSON数据:

Edit -> Preferences -> External Tools

我想将其转换为二维数组。 示例:

[{"TxtDate":"\/Date(1381343400000)\/","TxtCount":7},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":2},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":1},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":3},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":7},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":10},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":177},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":13},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":6},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":10},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":9},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":52},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":1},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":2},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":7},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":45},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":27},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":1},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":14},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":9},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":2},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":84},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":6},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":16},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":2},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":4},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":21},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":6},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":13},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":18},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":10},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":1},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":5},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":591}]

我尝试使用[[Date(1381343400000),7], [Date(1381343400000),2], ........] 方法,但它将其转换为对象数组。我如何实现这一目标?

1 个答案:

答案 0 :(得分:2)

您可以使用.map

var data = [
  {"TxtDate":"\/Date(1381343400000)\/","TxtCount":7},
  {"TxtDate":"\/Date(1381343400000)\/","TxtCount":2},
  {"TxtDate":"\/Date(1381343400000)\/","TxtCount":1}
];


var result = data.map(function (el) {
  return [
    new Date(+el.TxtDate.replace(/\/Date\((\d+)\)\//gi, '$1')), // converts to JavaScript Date
    el.TxtCount 
  ]
})

console.log(result);