typicaljoe / taffydb

TaffyDB - an open source JavaScript Database for your browser
http://taffydb.com
MIT License
2.21k stars 285 forks source link

Filtering array of objects that may or may not have a property #116

Open reloaded opened 8 years ago

reloaded commented 8 years ago

I have an array and I want to include the objects that don't have the "ignore" property or have it defined but it's set to "false". How can I do this?

The uncompiled source code is very hard to read and I couldn't figure it out... Nothing wrong with using expanded variable and function names to make it more readable...

The array structure is below.

array = [
{ ignore: false, name "someone" },
{ ignore: true, name "someone else" },
{ name: "yet another" }
]
mmikowski commented 8 years ago

Here is a simple JS construction that will work in all modern browsers:

#!/usr/bin/node

var
  old_list = [ 
    { ignore: false, name : "someone"      },  
    { ignore: true,  name : "someone else" },
    {                name : "yet another"  },  
    { ignore: false, name : "foo"          },  
    {                name : "baz"          }   
  ],  
  new_list
  ;
// args are list_data, idx, list
function fn_filter_x_ignore( list_data ) { 
  return ! list_data.ignore === true;
}

new_list = old_list.filter( fn_filter_x_ignore );
console.log( new_list );

Does that work for you?

And yes, it would be good to give TaffyDB a refresh and make sure it passes JSLint. I have a branched version does this, but haven't merged to master yet as we need more regression tests.

reloaded commented 8 years ago

Thanks @mmikowski . I ended up finding a documentation page on the official website that explained how to execute where conditions. The first doc page I went to (I think it was something like "getting started") did not link back to the API reference pages. I eventually found the more in depth docs and solved it :)

I could help contribute to a new major version/refactored version of TDB. Let me know if you guys are looking for the man power.

mmikowski commented 8 years ago

@reloaded we definitely need help. The key to refactoring is to get a good set of regression tests in place IMO. I've set that up with Nodeunit, but we need more tests. Could you help me write more tests? Like, for example, some that test this capability? That would be awesome, and would provide me the confidence needed to move forward with a revised version that would be much more testable and readable. Like taffy3. Notice the lowercase :)

reloaded commented 8 years ago

Is there a developer branch I can fork?