0

Is there any way of making this function recursive so that I do not need to create a switch for each length of filter criteria ?

var data = [
  {a:'aaa',b:'bbb',c:'ccc',d:'ddd',e:'eee'},
  {a:'aaa',b:'bbb',c:'ccc',d:'eee',e:'fff'},
  {a:'xxx',b:'bbb',c:'ccc',d:'ddd',e:'fff'}
]


function select(data,where){
  return data.filter(function(e){ 
    var k = Object.keys(where); 
    switch(k.length){     
      case 1: return (e[k[0]] == where[k[0]]);
      case 2: return (e[k[0]] == where[k[0]] && e[k[1]] == where[k[1]]);
      case 3: return (e[k[0]] == where[k[0]] && e[k[1]] == where[k[1]] && e[k[2]] == where[k[2]]);
      case 4: return (e[k[0]] == where[k[0]] && e[k[1]] == where[k[1]] && e[k[2]] == where[k[2]] && e[k[3]] == where[k[3]]);
      case 5: return (e[k[0]] == where[k[0]] && e[k[1]] == where[k[1]] && e[k[2]] == where[k[2]] && e[k[3]] == where[k[3]] && e[k[4]] == where[k[4]]);
    }
  })  
}

var where = {a:'aaa',b:'bbb'}

console.log(select(data,where));
4
  • This is so ugly, it looks almost like VHDL. Sorry, nothin' personal.. Commented Jan 10, 2015 at 0:07
  • Exactly, that's why I am asking the question, this works but it is very ugly - I don't think it's fair to give me negatices on this, my code is complete and my question is clear ! Commented Jan 10, 2015 at 0:10
  • Well it's very messy, I have trouble understanding what is the purpose / goal. Could you describe the desired behavior? Commented Jan 10, 2015 at 0:11
  • simple, select data from an object where the elements match the filter criteria, similar to SQL SELECT * FROM TABLE WHERE aaa ='aaa' and bbb = 'bbb' and.... Commented Jan 10, 2015 at 0:13

2 Answers 2

1

It doesn't need to be recursive (I'm not sure you understand what that means), you just need to loop on the elements in where:

function select(data, where) {
  return data.filter(function(e) {
    var k = Object.keys(where);
    return k.every(function(key) {
      return e[key] == where[key];
    });
  })
}

var data = [
  {a:'aaa',b:'bbb',c:'ccc',d:'ddd',e:'eee'},
  {a:'aaa',b:'bbb',c:'ccc',d:'eee',e:'fff'},
  {a:'xxx',b:'bbb',c:'ccc',d:'ddd',e:'fff'}
]

var where = {a:'aaa',b:'bbb'}

console.log(select(data,where));

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it seems obvious now, also I have never used every() before, that's new to me.
0

Try this code:

function select(data, where) {
    return data.filter(function (e) {
        for (var key in where) {
            if (where.hasOwnProperty(key)) {
                if (e.hasOwnProperty(key)) {
                    if (e[key] != where[key]) {
                        return false;
                    }
                }
                else {
                    return false
                }
            }
        }

        return true;
    })
}

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.