Consider:
var dummyArray = [{
"fname": "gali",
"lname": "doe"
}, {
"fname": "john",
"lname": "danny"
}, {
"fname": "joe",
"lname": "dawns"
}, {
"fname": "liji",
"lname": "hawk"
}]
dummyArray = j$.grep(dummyArray, function(dt) {
return (dt.fname== 'j');
});
How can I query the "fname" which contains letter "j" using the grep function?
Using the ECMAScript 5 standard function Array.filter
to only return the elements matching the predicate:
Starting with "j"
var j = dummyArray.filter(function(o) {
return o.fname.charAt(0) === 'j';
});
or, containing "j"
var j = dummyArray.filter(function(o) {
return o.fname.indexOf('j') >= 0;
});
You can use indexOf
:
dummyArray = $.grep(dummyArray, function(dt) {
return dt.fname.indexOf("j") != -1;
});
DEMO: http://jsfiddle.net/Nhmwk/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With