使用某些列

时间:2018-01-12 05:10:23

标签: javascript jquery

我有以下json数据。 我想使用url和skill以及yyyy_mm列进行过滤,并以json格式创建新数据。

我有两个要求: -

使用AND运算符:

  1. 使用url列过滤,yyyy_mm&技能专栏,假设我传递的网址为http://google.com,技能= C#和yyyy_mm = 2017-12 就像sql - 其中yyyy_mm ='2017-10'和技能='C#'和url喜欢'%google.com%'。然后它应该返回所有匹配的数据。
  2. 使用OR运算符:

    1. 使用网址过滤,yyyy_mm&技能专栏。因此,在这种情况下,应返回任何匹配的行。 就像sql - 其中yyyy_mm ='2017-12'和(技能='C#'或技能='vb.net')和url喜欢'%google.com%'。然后它应该返回所有匹配的数据。
    2. 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}
      ]
      

2 个答案:

答案 0 :(得分:0)

你可以拥有像这样的过滤功能



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}]

function filter(filterObj, andOr) {
    if(andOr === 'and')
        return Object.keys(filterObj).reduce(function(arr, key) {
            return arr.filter(function(data){
                return data[key] === filterObj[key]
            })
        }, tempData)
    else
        return Object.keys(filterObj).reduce(function(arr, key, index) {
            return (index != 0 ? arr : []).filter(function(data) {
                return data[key] !== filterObj[key]
            }).concat(
            tempData.filter(function(data) {
                return data[key] === filterObj[key]
            }))
        }, tempData)
}

console.log("with 'or' operation", filter({count:4, yyyy_mm: "2017-10"}, "or"))
console.log("with 'and' operation", filter({count:4, yyyy_mm: "2017-10"}, "and"))
console.log("with single", filter({count:4}, "and"))




答案 1 :(得分:0)

var result = [];
 getData("www.abcd.com","C#","1999-12");
function getData(url , skill , date){
   $.each(tempData,function(index,value){

     if(value.url == url && value.skills == skill && value.yyyy_mm == date ){
       result[index] = { url:value.url , skills:value.skills , yyyy_mm:value.yyyy_mm } ; 
     }else if(value.url == url){
        result[index] = {url:value.url}
     }elseif( ....)
       ....       
   })
}

我只给出了AND操作和休息的例子我用...代表你只需要在那里添加编码,然后休息就行了....