Click here to Skip to main content
15,898,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Posted
Updated 4-Jan-18 3:05am
Comments
F-ES Sitecore 4-Jan-18 8:28am    
Search for tutorials on using json from javascript

https://www.w3schools.com/js/js_json_arrays.asp

As it is your question reads simply as a request that we do your work for you.

1 solution

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
       }
 
Share this answer
 
v2
Comments
Member 11052432 4-Jan-18 11:10am    
Hi Thanks for the quick response.

I want to build data on JSON format as well like
var tempData = [{ "url": "http://google.com", "yyyy_mm": "2017-12", "skills": "C#", "count": 17 },
{ "url": "yahoo.com", "yyyy_mm": "2017-11", "skills": "vb.net", "count": 21 },
{ "url": "yahoo.com", "yyyy_mm": "2017-10", "skills": "json", "count": 111}];
Can you please give me the solution?

Many thanks !!!!!!
Karthik_Mahalingam 4-Jan-18 22:53pm    
how did you write google.com
since 2017-12 has google as well as yahoo.
Member 11052432 4-Jan-18 23:23pm    
var tempData = [{ "url": "http://google.com", "yyyy_mm": "2017-12", "skills": "C#", "count": 3 },
{ "url": "http://www.google.com", "yyyy_mm": "2017-12", "skills": "asp.ner", "count": 4 },
{ "url": "http://google.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 }];

Can you please provide the solution as below?
var tempData = [{ "url": "http://google.com", "yyyy_mm": "2017-12", "skills": "C#", "count": 17 },
{ "url": "yahoo.com", "yyyy_mm": "2017-11", "skills": "vb.net", "count": 21 },
{ "url": "yahoo.com", "yyyy_mm": "2017-10", "skills": "json", "count": 111}];
Karthik_Mahalingam 4-Jan-18 23:48pm    

        var tempData = [{ "url": "http://google.com", "yyyy_mm": "2017-12", "skills": "C#", "count": 3 },
 { "url": "http://www.google.com", "yyyy_mm": "2017-12", "skills": "asp.ner", "count": 4 },
 { "url": "http://google.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;
            var item = undefined;
            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
                {
                    if (count == 0)
                        item = tempData[j];
                    count += tempData[j].count;
                }
                if (item)
                    item.count = count;
            }
            finalArray.push(item)  // add the count to the final array
        }
Member 11052432 5-Jan-18 4:53am    
Thanks lot... it really worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900