简体   繁体   中英

Filtering JSON data in jQuery or vanilla JavaScript

I want to filter my JSON-object in jQuery or vanilla JavaScript. I prefer vanilla JavaScript but don't know how to solve this. I have created pseudo-code in order to explain what I want to achieve. Any ideas?

I basically want to only allow records that don't contain values from the lists. The first list only contains mfs values that should be excluded from expJSON. Second list contains pn values that should be excluded from expJSON, and so on...

Pseude-code

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

list1, list2, list3, list4, list5

              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", ...]

If I understand the question correctly, this should solve it for you.

// 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;
           }
        }
    });
});

I'm guessing you mean something like the following?

I just simply used Array.filter to file through each Object in the JSON Object. Then a simple switch statement within a for loop for each item in the object in order to "filter" them into the proper list.


Update! After further review of your question and what you asked in comments, I believe what you're seeking is something more like the following. I changed the loop to instead only allow objects to return where said sub items cannot be found in given list. Hope this is what you're seeking! Good Luck!

 var expJSON = [{"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"}]; function filterOutOfMainList(obj, ind, $this) { for (var x in obj) switch(x) { case 'mft': if (list1.indexOf(obj[x]) > -1) return; break; case 'pn': if (list2.indexOf(obj[x]) > -1) return; break; case 'vtv': if (list3.indexOf(obj[x]) > -1) return; break; case 'psn': if (list4.indexOf(obj[x]) > -1) return; break; case 'sl': if (list5.indexOf(obj[x]) > -1) return; break; } return obj; } var list1 = ["example->", "fsjldfd98sf9d"], // mft list2 = ["word1", "word2", "etc"], // pn list3 = ["Yes"], // vtv list4 = ["word1", "word2", "etc"], // psn list5 = ["word1", "word2", "etc"]; // sl console.log(expJSON.filter(filterOutOfMainList)); 
 <h1>Using your JSON as example. Vanilla!</h1> 

So here's some very basic and not well thought out unoptimized code that should do what you want:

function getLists(data){
  var output={}
  var keys = ['mft', 'pn', 'vtv', 'psn', 'sl'];
  for(var i = 0; i < keys.length; i++){
    var current = keys[i];
    output[current] = [];
    for(var j = 0; j < data.length; j++){
      if(data[j][current){
        output[current].push(data[j][current])
      }
    }
  }
  return output;
}

This function should output something like {mft: ['word1', 'word2' ...], pn: ['word1', 'word2' ...] ... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM