Skip to content

Instantly share code, notes, and snippets.

@nujabes
Created February 4, 2015 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nujabes/4610a2f8da075b663c81 to your computer and use it in GitHub Desktop.
Save nujabes/4610a2f8da075b663c81 to your computer and use it in GitHub Desktop.
filter multi-directional array with an array value. // source http://jsbin.com/xeziki
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="filter multi-directional array with an array value." />
<script src="http://jashkenas.github.io/underscore/underscore-min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
// filter multi-directional array with an array value.
var array = [
{'name':'one', "age":'3'},
{'name':'two', "age":'1'},
{'name':'three', "age":'3'},
{'name':'four', "age":'1'},
{'name':'one', "age":'7'},
{'name':'one', "age":'5'},
{'name':'one', "age":'7'},
{'name':'one', "age":'8'},
{'name':'one', "age":'7'},
{'name':'one', "age":'11'},
{'name':'one', "age":'7'}
];
var filterarray = ['7', '8'];
var result = _.filter(array, function(item){
// _filter will pass if the function return true
return filterarray.indexOf(item.age) != -1;
// Returns the index at which value can be found in the array, or -1 if value is not present in the array.
});
console.log(result);
</script>
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html>
<html>
<head>
<meta name="description" content="filter multi-directional array with an array value." />
<script src="//jashkenas.github.io/underscore/underscore-min.js"><\/script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
</body>
</html></script>
<script id="jsbin-source-javascript" type="text/javascript">// filter multi-directional array with an array value.
var array = [
{'name':'one', "age":'3'},
{'name':'two', "age":'1'},
{'name':'three', "age":'3'},
{'name':'four', "age":'1'},
{'name':'one', "age":'7'},
{'name':'one', "age":'5'},
{'name':'one', "age":'7'},
{'name':'one', "age":'8'},
{'name':'one', "age":'7'},
{'name':'one', "age":'11'},
{'name':'one', "age":'7'}
];
var filterarray = ['7', '8'];
var result = _.filter(array, function(item){
// _filter will pass if the function return true
return filterarray.indexOf(item.age) != -1;
// Returns the index at which value can be found in the array, or -1 if value is not present in the array.
});
console.log(result);
</script></body>
</html>
// filter multi-directional array with an array value.
var array = [
{'name':'one', "age":'3'},
{'name':'two', "age":'1'},
{'name':'three', "age":'3'},
{'name':'four', "age":'1'},
{'name':'one', "age":'7'},
{'name':'one', "age":'5'},
{'name':'one', "age":'7'},
{'name':'one', "age":'8'},
{'name':'one', "age":'7'},
{'name':'one', "age":'11'},
{'name':'one', "age":'7'}
];
var filterarray = ['7', '8'];
var result = _.filter(array, function(item){
// _filter will pass if the function return true
return filterarray.indexOf(item.age) != -1;
// Returns the index at which value can be found in the array, or -1 if value is not present in the array.
});
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment