我想用jQuery或普通JavaScript过滤JSON对象。我更喜欢普通JavaScript,但不知道如何解决。我已经创建了伪代码来解释我想要实现的目标。有任何想法吗?

我基本上只希望允许不包含列表中值的记录。第一个列表仅包含应从expJSON中排除的mfs值。第二个列表包含应从expJSON中排除的pn值,依此类推...

伪代码

var results = $(jsonData).filter(function (i, n) {
        return if list1 is not empty: (n.psn.indexOf(item1 from list1) === -1 || n.psn.indexOf(item2 from list1) === -1 || n.psn.indexOf(item3 from list1) === -1 ... ) &&
        if list2 is not empty: (n.pn.indexOf(item1 from list2) === -1 || n.pn.indexOf(item2 from list2) === -1 || n.pn.indexOf(item3 from list2) === -1 ... ) &&
        if list3 is not empty: (n.mft.indexOf(item1 from list3) === -1 || n.mft.indexOf(item2 from list3) === -1 || n.mft.indexOf(item3 from list3) === -1 ... ) &&
        if list4 is not empty: (n.sl.indexOf(item1 from list4) === -1 || n.sl.indexOf(item2 from list4) === -1 || n.sl.indexOf(item3 from list4) === -1 ... ) &&
        if list5 is not empty: (n.vtv.indexOf(item1 from list5) === -1 || n.vtv.indexOf(item2 from list5) === -1 || n.vtv.indexOf(item3 from list5) === -1 ... )
    })


jsonData

[{"mft": "asjfasdf", "pn": "234awefwa", "vtv": "No", "psn": "234fasdfa", "sl": "asf8sf"}, {"mft": "fsjldfd98sf9d", "pn": "skfjsdf7df", "vtv": "Yes", "psn": "tfs76fdfd", "sl": "basd7f"}, {"mft": "fbsdf8df", "pn": "898723923", "vtv": "No", "psn": "fs7daf6sd", "sl": "f7s6df"}, {"mft": "sdf7688sdf76f", "pn": "131d21", "vtv": "Yes", "psn": "t23yt342y23", "sl": "bfldk34"} ...]


列表1,列表2,列表3,列表4,列表5

              item1     item2    item3
list1/mft = ["word1", "word2", "word3", ...]
list2/pn = ["word1", "word2", "word3", ...]
list3/vtv = ["word1", "word2", "word3", ...]
list4/psn = ["word1", "word2", "word3", ...]
list5/sl = ["word1", "word2", "word3", ...]

最佳答案

如果我正确理解了这个问题,这应该可以为您解决。

// this is based on the jsonData provided
// but I deleted some values cause you said it might not always have a value
var jsonData = [
    {"mft": "asjfasdf", "pn": "234awefwa", "vtv": "", "psn": "234fasdfa", "sl": "asf8sf"},
    {"mft": "fsjldfd98sf9d", "pn": "skfjsdf7df", "vtv": "Yes", "psn": "tfs76fdfd", "sl": "basd7f"},
    {"mft": "", "pn": "898723923", "vtv": "No", "psn": "fs7daf6sd", "sl": "f7s6df"},
    {"mft": "sdf7688sdf76f", "pn": "", "vtv": "Yes", "psn": "t23yt342y23", "sl": "bfldk34"},
    {"mft": "sdf7688sdf76f", "pn": "131d21", "vtv": "Yes", "psn": "t23yt342y23", "sl": ""}]

var keys = ['mft', 'pn', 'vtv', 'psn', 'sl']
var mftList = []
var pnList = []
var vtvList = []
var psnList = []
var slList = []

jsonData.forEach(function(data) {
    keys.forEach(function(key){
       var value = data[key];
       if(!value) {return;}

       switch (key) {
           case 'mft': {
                mftList.push(value)
                break;
           }
           case 'pn': {
                pnList.push(value)
                break;
           }
           case 'vtv': {
                vtvList.push(value)
                break;
           }
           case 'psn': {
                psnList.push(value)
                break;
           }
           case 'sl': {
                slList.push(value)
                break;
           }
        }
    });
});

07-26 06:43