过滤json数据并总结它 [英] Filtering json data and summing it up

查看:85
本文介绍了过滤json数据并总结它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有以下Json数据: -



 var tempData = [{url:http://google.com,yyyy_mm:2017-12,技能:C#,count:3},
{url:www.yahoo.com,yyyy_mm:2017-12,技能:asp.ner,count:4},
{url :yahoo.com,yyyy_mm:2017-12,技能:vb.net,count:10},
{url:yahoo.com, yyyy_mm:2017-11,技能:vb.net,计数:5},
{url:yahoo.com,yyyy_mm:2017-11 ,技能:vb.net,count:6},
{url:yahoo.com,yyyy_mm:2017-11,技能:vb .net,count:10},
{url:yahoo.com,yyyy_mm:2017-10,技能:vb.net,count: 11},
{url:yahoo.com,yyyy_mm:2017-10,技能:json,count:100}]





我必须通过'yyyy_mm'列过滤并计算'count'列的总和,如

假设,yyyy_mm = 2017 -12然后算w在2017-11赛季应该是17

,计算是21

和2017-10,计数= 111等......

并再次创建新的json数据。

如何完成此操作?




提前致谢。



我尝试了什么:



我试过但无法实现。

解决方案

尝试

查看内联评论

 var tempData = [{url :http://google.com,yyyy_mm:2017-12,技能:C#,计数:3},
{url:www.yahoo .com,yyyy_mm:2017-12,技能:asp.ner,count:4},
{url:yahoo.com,yyyy_mm: 2017-12,技能:vb.net,计数:10},
{url:yahoo.com,yyyy_mm:2017-11,技能:vb.net,count:5},
{url:yahoo.com,yyyy_mm:2017-11,技能:vb.net, count:6},
{url:yahoo.com,yyyy_mm:201 7-11,技能:vb.net,计数:10},
{url:yahoo.com,yyyy_mm:2017-10,技能 :vb.net,count:11},
{url:yahoo.com,yyyy_mm:2017-10,技能:json,count :100}];

var distinct = []; //用于存储唯一日期的数组
for(var i = 0; i< tempData.length; i ++){//循环查找唯一日期
var item = tempData [i];
if(distinct.indexOf(item.yyyy_mm)== -1)//参考indexOf - Google
distinct.push(item.yyyy_mm);
}
var finalArray = []; //数组存储最终输出
for(var i = 0; i< distinct.length; i ++){
var count = 0;
for(var j = 0; j< tempData.length; j ++){
if(distinct [i] == tempData [j] .yyyy_mm)//从主数组中找到匹配的项目关于来自不同
count + = tempData [j] .count的每个项目;
}
finalArray.push({yyyy_mm:distinct [i],count:count})//将计数添加到最终数组
}


Hi All,

I have a Json data like below :-

var tempData =[{"url":"http://google.com","yyyy_mm":"2017-12","skills":"C#","count":3},
               {"url":"www.yahoo.com","yyyy_mm":"2017-12","skills":"asp.ner","count":4},
               {"url":"yahoo.com","yyyy_mm":"2017-12","skills":"vb.net","count":10},
               {"url":"yahoo.com","yyyy_mm":"2017-11","skills":"vb.net","count":5},
               {"url":"yahoo.com","yyyy_mm":"2017-11","skills":"vb.net","count":6},
               {"url":"yahoo.com","yyyy_mm":"2017-11","skills":"vb.net","count":10},
               {"url":"yahoo.com","yyyy_mm":"2017-10","skills":"vb.net","count":11},
               {"url":"yahoo.com","yyyy_mm":"2017-10","skills":"json","count":100}]



I have to filter through 'yyyy_mm' column and calculate sum of 'count' column like
suppose, yyyy_mm = 2017-12 then count would be 17
similarly for 2017-11, count would be 21
and for 2017-10, count = 111 and so....
and again create the new json data.
How to accomplish this?


Thanks in advance.

What I have tried:

I tried but not able to achieve.

解决方案

try
check the inline comments

var tempData = [{ "url": "http://google.com", "yyyy_mm": "2017-12", "skills": "C#", "count": 3 },
             { "url": "www.yahoo.com", "yyyy_mm": "2017-12", "skills": "asp.ner", "count": 4 },
             { "url": "yahoo.com", "yyyy_mm": "2017-12", "skills": "vb.net", "count": 10 },
             { "url": "yahoo.com", "yyyy_mm": "2017-11", "skills": "vb.net", "count": 5 },
             { "url": "yahoo.com", "yyyy_mm": "2017-11", "skills": "vb.net", "count": 6 },
             { "url": "yahoo.com", "yyyy_mm": "2017-11", "skills": "vb.net", "count": 10 },
             { "url": "yahoo.com", "yyyy_mm": "2017-10", "skills": "vb.net", "count": 11 },
             { "url": "yahoo.com", "yyyy_mm": "2017-10", "skills": "json", "count": 100 }];

       var distinct = []; // array to store unique dates
       for (var i = 0; i < tempData.length; i++) {  // loop to find unique date
           var item = tempData[i];
           if (distinct.indexOf(item.yyyy_mm) == -1)  // refer indexOf - Google
               distinct.push(item.yyyy_mm);
       }
       var finalArray = [];  // array to store final output
       for (var i = 0; i < distinct.length; i++) {
           var count = 0;
           for (var j = 0; j < tempData.length; j++) {
               if (distinct[i] == tempData[j].yyyy_mm)  // find the matching items form the main array with respect to each item from distinct
                   count += tempData[j].count;
           }
           finalArray.push({"yyyy_mm":distinct[i],"count":count})  // add the count to the final array
       }


这篇关于过滤json数据并总结它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆