0

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", ...]
5
  • 1
    I am a little unclear as yo what you want, could you make your jsonData match with your lists so that we can accurately see the before/after? Commented Mar 31, 2017 at 16:21
  • I want to show records from jsonData that dont contain values from the lists. Each list contains values for a key in the json records. Commented Mar 31, 2017 at 16:29
  • I have updated the list names Commented Mar 31, 2017 at 16:36
  • Does each object always have the same keys? Will every object always have mft, pn, vtv, psn, and sl? Commented Mar 31, 2017 at 16:37
  • Yes, but a list can be empty Commented Mar 31, 2017 at 16:39

3 Answers 3

1

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;
           }
        }
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

It seems like your lists are output. But in my case I want them to be input lists. 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...
Please provide an example that shows the exact data you want to put in, and what it should look like when its done.
1

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>

3 Comments

For my understanding, do you filter out all records from expJSON that don't contain values from list1, list2, list3, list4, list5?
It seems like your lists are output. But in my case I want them to be input lists. 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...
@Engo I updated my answer and changed my solution to filter in reverse, using given list. I went ahead and filled out a couple example words so that it will change the final result of your JSON as seen in the console when you run it. Good luck!
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.