JSON对象过滤器

时间:2017-12-19 19:10:37

标签: javascript json

我是json的新手,面临一个过滤问题。

我在json之下;

var data =[
   {
      "year":2015,
      "month":1,
      "total_sales":123
   },
   {
      "year":2015,
      "month":2,
      "total_sales":212
   },
   {
      "year":2015,
      "month":3,
      "total_sales":602
   },
   {
      "year":2015,
      "month":4,
      "total_sales":918
   },
   {
      "year":2015,
      "month":5,
      "total_sales":920
   },
   {
      "year":2015,
      "month":6,
      "total_sales":167
   },
   {
      "year":2015,
      "month":7,
      "total_sales":922
   },
   {
      "year":2015,
      "month":8,
      "total_sales":162
   },
   {
      "year":2015,
      "month":9,
      "total_sales":678
   },
   {
      "year":2015,
      "month":10,
      "total_sales":312
   },
   {
      "year":2015,
      "month":11,
      "total_sales":895
   },
   {
      "year":2015,
      "month":12,
      "total_sales":470
   },
   {
      "year":2016,
      "month":1,
      "total_sales":962
   },
   {
      "year":2016,
      "month":2,
      "total_sales":501
   },
   {
      "year":2016,
      "month":3,
      "total_sales":115
   },
   {
      "year":2016,
      "month":4,
      "total_sales":193
   },
   {
      "year":2016,
      "month":5,
      "total_sales":199
   },
   {
      "year":2016,
      "month":6,
      "total_sales":169
   },
   {
      "year":2016,
      "month":7,
      "total_sales":482
   },
   {
      "year":2016,
      "month":8,
      "total_sales":197
   },
   {
      "year":2016,
      "month":9,
      "total_sales":375
   },
   {
      "year":2016,
      "month":10,
      "total_sales":497
   },
   {
      "year":2016,
      "month":11,
      "total_sales":692
   },
   {
      "year":2016,
      "month":12,
      "total_sales":868
   },
   {
      "year":2017,
      "month":1,
      "total_sales":292
   },
   {
      "year":2017,
      "month":2,
      "total_sales":602
   },
   {
      "year":2017,
      "month":3,
      "total_sales":744
   },
   {
      "year":2017,
      "month":4,
      "total_sales":248
   },
   {
      "year":2017,
      "month":5,
      "total_sales":549
   },
   {
      "year":2017,
      "month":6,
      "total_sales":694
   },
   {
      "year":2017,
      "month":7,
      "total_sales":793
   },
   {
      "year":2017,
      "month":8,
      "total_sales":436
   },
   {
      "year":2017,
      "month":9,
      "total_sales":752
   },
   {
      "year":2017,
      "month":10,
      "total_sales":852
   },
   {
      "year":2017,
      "month":11,
      "total_sales":588
   },
   {
      "year":2017,
      "month":12,
      "total_sales":807
   }
]

我希望以下面的格式减少它;

var data = [
   {
      "month":"january",
      "2015":123,
      "2016":962,
      "2017":292
   },
   {
      "month":"february",
      "2015":212,
      "2016":30,
      "2017744 602 },{ "      month":"march",
      "2015":212,
      "2016":30,
      "2017":20
   }   ....
];

到目前为止已经尝试过:

var uniqueYears = [];
for (i = 0; i < data.length; i++) {
    if (uniqueYears.indexOf(data[i].year) === -1) {
        uniqueYears.push(data[i].year);
    }
}
var uniqueMonths = [];
for (i = 0; i < data.length; i++) {
    if (uniqueMonths.indexOf(data[i].month) === -1) {
        uniqueMonths.push(data[i].month);
    }
}
var attributelist = [];
console.log(data.length);
for (i = 0; i < data.length; i++) {
    var as = $(data).filter(function(i, n) {
        console.log(n) return n.month === uniqueMonths[i]
    });
}  

先谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用每月对象的预定义数组,然后迭代给定的对象数组,并将找到的总值分配给月份项目,其中年份为关键,总计为值。

&#13;
&#13;
var array = [{ year: 2015, month: 1, total_sales: 123 }, { year: 2015, month: 2, total_sales: 212 }, { year: 2015, month: 3, total_sales: 602 }, { year: 2015, month: 4, total_sales: 918 }, { year: 2015, month: 5, total_sales: 920 }, { year: 2015, month: 6, total_sales: 167 }, { year: 2015, month: 7, total_sales: 922 }, { year: 2015, month: 8, total_sales: 162 }, { year: 2015, month: 9, total_sales: 678 }, { year: 2015, month: 10, total_sales: 312 }, { year: 2015, month: 11, total_sales: 895 }, { year: 2015, month: 12, total_sales: 470 }, { year: 2016, month: 1, total_sales: 962 }, { year: 2016, month: 2, total_sales: 501 }, { year: 2016, month: 3, total_sales: 115 }, { year: 2016, month: 4, total_sales: 193 }, { year: 2016, month: 5, total_sales: 199 }, { year: 2016, month: 6, total_sales: 169 }, { year: 2016, month: 7, total_sales: 482 }, { year: 2016, month: 8, total_sales: 197 }, { year: 2016, month: 9, total_sales: 375 }, { year: 2016, month: 10, total_sales: 497 }, { year: 2016, month: 11, total_sales: 692 }, { year: 2016, month: 12, total_sales: 868 }, { year: 2017, month: 1, total_sales: 292 }, { year: 2017, month: 2, total_sales: 602 }, { year: 2017, month: 3, total_sales: 744 }, { year: 2017, month: 4, total_sales: 248 }, { year: 2017, month: 5, total_sales: 549 }, { year: 2017, month: 6, total_sales: 694 }, { year: 2017, month: 7, total_sales: 793 }, { year: 2017, month: 8, total_sales: 436 }, { year: 2017, month: 9, total_sales: 752 }, { year: 2017, month: 10, total_sales: 852 }, { year: 2017, month: 11, total_sales: 588 }, { year: 2017, month: 12, total_sales: 807 }],
    grouped = [{ month: 'january' }, { month: 'february' }, { month: 'march' }, { month: 'april' }, { month: 'may' }, { month: 'june' }, { month: 'july' }, { month: 'august' }, { month: 'september' }, { month: 'october' }, { month: 'november' }, { month: 'december' }];

array.forEach(function (o) {
    grouped[o.month - 1][o.year] = o.total_sales;
});

console.log(grouped);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;