Javascript JSON基于日期处理的数据

时间:2015-12-18 08:25:15

标签: javascript arrays json jsonobject

我已经给出了下面的数组。我的问题是如何根据日期处理数据。 例如:低于服务的响应。

[[
  {"claimNumber":"6396","itemNumber":"1","dateAssessed":"20/10/2015"},
  {"claimNumber":"2208","itemNumber":"1","dateAssessed":"20/09/2015"},
  {"claimNumber":"918","itemNumber":"1","dateAssessed":"12/09/2015"},
  {"claimNumber":"2208","itemNumber":"1","dateAssessed":"20/09/2015"},
  {"claimNumber":"2206","itemNumber":"1","dateAssessed":"20/09/2015"},
  {"claimNumber":"2205","itemNumber":"1","dateAssessed":"20/09/2015"},
  {"claimNumber":"2208","itemNumber":"1","dateAssessed":"20/09/2015"},
]]

我想根据日期处理数据如下:

20/09/2015

  {"claimNumber":"2208","itemNumber":"1","dateAssessed":"20/09/2015"},
  {"claimNumber":"2206","itemNumber":"1","dateAssessed":"20/09/2015"},
  {"claimNumber":"2205","itemNumber":"1","dateAssessed":"20/09/2015"},
  {"claimNumber":"2208","itemNumber":"1","dateAssessed":"20/09/2015"}

20/10/2015

   {"claimNumber":"6396","itemNumber":"1","dateAssessed":"20/10/2015"}

2015年12月9日

  {"claimNumber":"918","itemNumber":"1","dateAssessed":"12/09/2015"}

先谢谢。

4 个答案:

答案 0 :(得分:1)

为此做一些安排功能。 arrange()返回对象中的分组数据。



array (size=27)
   0 => string 'http://php.net' (length=14)
   1 => string '
      http://php.net/downloads' (length=26)
   2 => string '
       http://php.net/docs.php' (length=25)
   3 => string '
       http://php.net/get-involved' (length=29)




答案 1 :(得分:0)

好吧,我会这样想:

  • 按日期排序数据,
  • 创建新的空对象newData,
  • 遍历数据数组,
    • data = data [i] .date;
    • 检查newData [data]是否存在:如果不存在,则使用值为空的数组
    • 创建它
    • 将数据[i]添加到newData [data]

我没有提及转换等类型。

答案 2 :(得分:0)

您可以使用.filter

function reduceArr(x, date){
    return x[0].filter(function(elem){
        return elem.dateAssessed==date;
    });
}

console.log(reduceArr(x, "20/09/2015"));

这将返回所需对象的数组。

Working Fiddle

答案 3 :(得分:0)

不清楚您的问题,但如果您想按日期对其进行分组,则:

var arr=[
  {"claimNumber":"6396","itemNumber":"1","dateAssessed":"20/10/2015"},
  {"claimNumber":"2208","itemNumber":"1","dateAssessed":"20/09/2015"},
  {"claimNumber":"918","itemNumber":"1","dateAssessed":"12/09/2015"},
  {"claimNumber":"2208","itemNumber":"1","dateAssessed":"20/09/2015"},
  {"claimNumber":"2206","itemNumber":"1","dateAssessed":"20/09/2015"},
  {"claimNumber":"2205","itemNumber":"1","dateAssessed":"20/09/2015"},
  {"claimNumber":"2208","itemNumber":"1","dateAssessed":"20/09/2015"},
];
var newarr = {};
for(var i=0; i<arr.length;i++){
    newarr[arr[i].dateAssessed] = newarr[arr[i].dateAssessed] || [];
    newarr[arr[i].dateAssessed].push(arr[i]);
}