I have an array of objects and I\'m wondering the best way to search it. Given the below example how can I search for name = \"Joe\"
and age < 30
? Is there anything jQuery can help with or do I have to brute force this search myself?
var names = new Array();
var object = { name : \"Joe\", age:20, email: \"joe@hotmail.com\"};
names.push(object);
object = { name : \"Mike\", age:50, email: \"mike@hotmail.com\"};
names.push(object);
object = { name : \"Joe\", age:45, email: \"mike@hotmail.com\"};
names.push(object);
You may use jQuery.grep()
:
var found_names = $.grep(names, function(v) {
return v.name === \"Joe\" && v.age < 30;
});
DEMO: http://jsfiddle.net/ejPV4/
You can do this very easily with the [].filter
method:
var filterednames = names.filter(function(obj) {
return (obj.name === \"Joe\") && (obj.age < 30);
});
You will need to add a shim for browsers that don\'t support the [].filter
method: this MDN page gives such code.
You could utilize jQuery.filter() function to return elements from a subset of the matching elements.
var names = [
{ name : \"Joe\", age:20, email: \"joe@hotmail.com\"},
{ name : \"Mike\", age:50, email: \"mike@hotmail.com\"},
{ name : \"Joe\", age:45, email: \"mike@hotmail.com\"}
];
var filteredNames = $(names).filter(function( idx ) {
return names[idx].name === \"Joe\" && names[idx].age < 30;
});
$(filteredNames).each(function(){
$(\'#output\').append(this.name);
});
<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js\"></script>
<div id=\"output\"/>
var nameList = [
{name:\'x\', age:20, email:\'x@email.com\'},
{name:\'y\', age:60, email:\'y@email.com\'},
{name:\'Joe\', age:22, email:\'joe@email.com\'},
{name:\'Abc\', age:40, email:\'abc@email.com\'}
];
var filteredValue = nameList.filter(function (item) {
return item.name == \"Joe\" && item.age < 30;
});
//To See Output Result as Array
alert(JSON.stringify(filteredValue));
You can simply use javascript :)
So quick question. What if you have two arrays of objects and you would like to \'align\' these object arrays so that you can make sure each array\'s objects are in the order as the other array\'s? What if you don\'t know what keys and values any of the objects inside of the arrays contains... Much less what order they\'re even in?
So you need a \'WildCard Expression\' for your [].filter
, [].map
, etc. How do you get a wild card expression?
var jux = (function(){
\'use strict\';
function wildExp(obj){
var keysCrude = Object.keys(obj),
keysA = (\'a[\"\' + keysCrude.join(\'\"], a[\"\') + \'\"]\').split(\', \'),
keysB = (\'b[\"\' + keysCrude.join(\'\"], b[\"\') + \'\"]\').split(\', \'),
keys = [].concat(keysA, keysB)
.sort(function(a, b){ return a.substring(1, a.length) > b.substring(1, b.length); });
var exp = keys.join(\'\').split(\']b\').join(\'] > b\').split(\']a\').join(\'] || a\');
return exp;
}
return {
sort: wildExp
};
})();
var sortKeys = {
k: \'v\',
key: \'val\',
n: \'p\',
name: \'param\'
};
var objArray = [
{
k: \'z\',
key: \'g\',
n: \'a\',
name: \'b\'
},
{
k: \'y\',
key: \'h\',
n: \'b\',
name: \'t\'
},
{
k: \'x\',
key: \'o\',
n: \'a\',
name: \'c\'
}
];
var exp = jux.sort(sortKeys);
console.log(\'@juxSort Expression:\', exp);
console.log(\'@juxSort:\', objArray.sort(function(a, b){
return eval(exp);
}));
You can also use this function over an iteration for each object to create a better collective expression for all of the keys in each of your objects, and then filter your array that way.
This is a small snippet from the API Juxtapose which I have almost complete, which does this, object equality with exemptions, object unities, and array condensation. If these are things you need or want for your project please comment and I\'ll make the lib accessible sooner than later.
Hope this helps! Happy coding :)