Click here to Skip to main content
15,920,513 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi All,

I have below json data.
I want to filter using url and yyyy_mm column and create the new data in json format.

i have 2 requirements here:-
1). filter using url column, suppose if i pass url as http://google.com then it should return me all data which contains google.com.

2). filter using url and yyyy_mm column. So in this case whatever matching rows should be returned.
Please note that :- it should return me json object.


var tempData =[{"url":"http://google.com","yyyy_mm":"2017-12","skills":"C#","count":3},                    {"url":"http://google.com","yyyy_mm":"2017-11","skills":"F#","count":4}
{"url":"http://google.com","yyyy_mm":"2017-10","skills":"asp.net","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}]


Thanks in Advance!!!!!

What I have tried:

I tried using $.grep but did not achieve anything. In some cases $.grep is not working.
I am using Node.JS and creating html tags dynamically and showing data.
Posted
Updated 4-Jan-18 16:52pm

1 solution

try

var tempData = [{ "url": "http://google.com", "yyyy_mm": "2017-12", "skills": "C#", "count": 3 },
           { "url": "http://google.com", "yyyy_mm": "2017-11", "skills": "F#", "count": 4 },
       { "url": "http://google.com", "yyyy_mm": "2017-10", "skills": "asp.net", "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 }]


       function filter(data, url, date) {
           var result = [];
           if (url)
               result = data.filter(function (item) { return item.url == url });
           if (date)
               result = result.filter(function (item) { return item.yyyy_mm == date });
           return result;
       }

       var data1 = filter(tempData, 'yahoo.com');
       var data2 = filter(tempData, 'yahoo.com', '2017-10');
       debugger


refer JavaScript Array filter() Method[^]
 
Share this answer
 

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