Created
February 4, 2015 05:19
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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