我有以下json数据。 我想使用url和skill以及yyyy_mm列进行过滤,并以json格式创建新数据。
我有两个要求: -
使用AND运算符:
使用OR运算符:
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}
]
答案 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操作和休息的例子我用...代表你只需要在那里添加编码,然后休息就行了....