本文介绍了使用某些列过滤JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 我有json数据以下。 我想过滤使用网址和技巧以及yyyy_mm列并以json格式创建新数据。 我这里有2个要求: - 使用AND运算符: 1)。使用url列过滤,yyyy_mm&技能专栏,假设我将网址传递给http://google.com并且技巧= C#和yyyy_mm = 2017-12 就像sql一样 - 其中yyyy_mm =' 2017-10'和技能='C#'和url喜欢'%google.com%'。然后它应该返回所有匹配的数据。 使用OR运算符: 2)。使用网址过滤,yyyy_mm&技能专栏。所以在这种情况下应该返回任何匹配的行。 就像sql - 其中yyyy_mm ='2017-12'和(技能='C#'或技能=' vb.net')和url喜欢'%google.com%'。然后它应该返回所有匹配的数据。 var tempData = [{url:http:/ /google.com/shjj/player\",\"yyyy_mm\":\"2017-12\",\"skills\":\"C#\",\"count\":3},{url:http://google.com, yyyy_mm:2017-12,技能:C#,计数:4} {url:http://google.com/jkl/uop,yyyy_mm :2017-10,技能:C#,count:4},{url:yahoo.com,yyyy_mm:2017-12,技能:vb。 net,count:10}, {url:yahoo.com,yyyy_mm:2017-11,技能:vb.net,count:5 }, {url:https://yahoo.com/player/fast,yyyy_mm:2017-11,技能: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,技能:vb.net, count:11}, {url:yahoo.com,yyyy_mm:2017-10,技能:json,count:100} { url:yahoo.com/onj/sfd,yyyy_mm:2017-10,技能:C#,count:200}] 我是什么尝试过: 我尝试使用indexof方法但是如何申请或运营。解决方案 尝试 函数过滤器(数据,网址,技能,日期){ 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); } 返回结果; } var data1 = filter(tempData,'google.com','C#','2017-12') var data2 = filter(tempData,'yahoo。 com',['C#','vb.net'],'2017-12') 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-12Just 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. 解决方案 tryfunction 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') 这篇关于使用某些列过滤JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-21 06:45