Click here to Skip to main content
15,442,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

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

I have 2 requirements here:-
Using AND operator:
1). filter using url column, yyyy_mm & skills column, suppose if i pass url as http://google.com and skills = C# and yyyy_mm = 2017-12

Just like sql - where yyyy_mm = '2017-10' and skills = 'C#' and url like '%google.com%'. Then it should return me all matching data.

Using OR operator:
2). filter using url , yyyy_mm & skills column. So in this case whatever matching rows should be returned.

Just like sql - where yyyy_mm = '2017-12' and (skills = 'C#' OR skills = 'vb.net') and url like '%google.com%'. Then it should return me all matching data.


var tempData =[{"url":"http://google.com/shjj/player","yyyy_mm":"2017-12","skills":"C#","count":3},                    {"url":"http://google.com","yyyy_mm":"2017-12","skills":"C#","count":4}
{"url":"http://google.com/jkl/uop","yyyy_mm":"2017-10","skills":"C#","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":"https://yahoo.com/player/fast","yyyy_mm":"2017-11","skills":"vb.net","count":6},

{"url":"google.com/lope/fgkl","yyyy_mm":"2017-10","skills":"vb.net","count":10},

{"url":"http://google.com/gog/blab","yyyy_mm":"2017-10","skills":"vb.net","count":11},
{"url":"yahoo.com","yyyy_mm":"2017-10","skills":"json","count":100}
{"url":"yahoo.com/onj/sfd","yyyy_mm":"2017-10","skills":"C#","count":200}]


What I have tried:

I tried with indexof method but how to apply or operator.
Posted
Updated 11-Jan-18 19:11pm

1 solution

try

function filter(data, url, skills, date) {
          url = url.toLowerCase();
          skills = skills.map(function (a) { return a.toLowerCase() });
          var result = [];
          for (var i = 0; i < data.length; i++) {
              var item = data[i];
              if (item.url.toLowerCase().indexOf(url) > -1)
                  if (item.yyyy_mm == date)
                      if (skills.indexOf(item.skills.toLowerCase()) > -1)
                          result.push(item);
          }
          return result;
      }

      var data1 = filter(tempData, 'google.com', 'C#', '2017-12')
      var data2 = filter(tempData, 'yahoo.com', ['C#', 'vb.net'], '2017-12')
 
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