PDA

View Full Version : Select from array, is it possible?



yuryn1961
20 Mar 2012, 11:06 AM
Hi,

I've got a javascript array:

var x = [
{id:22, name:'Merry'},
{id:26, name:'Poppins'},
{id:345, name:'Popcorn'}
];

Is it possible to create with Sencha Core a method which returns a sub array by a given param, like SQL command (select elements where name like '%Pop%').

function x(x, 'Pop') { } will returns:
[
{id:26, name:'Poppins'},
{id:345, name:'Popcorn'}
}.
Any help will be appreciate.
Thanks

mitchellsimoens
20 Mar 2012, 11:14 AM
You can use RegEx but you need something to look at the object. We have one for an array of strings.

burnnat
20 Mar 2012, 11:15 AM
What you're looking for, I think, is the function Ext.Array.filter (http://docs.sencha.com/ext-js/4-0/#%21/api/Ext.Array-method-filter) which can be used like so:


var pops = Ext.Array.filter(array, function(item) {
return item.name.indexOf('Pop') != -1;
});

LesJ
20 Mar 2012, 12:13 PM
You can also take a look at the MixedCollection class, which is a beefed up Array.

See how you can filter data in stores:

http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.Store-method-filter

yuryn1961
20 Mar 2012, 1:16 PM
Thanks burnnat, I tried it, it does not work with Ext Core (as the LesJ's solution), but your reply helped me.
Here is my code:
function selectFromArray(arr, s){
var result=new Array();
Ext.each(arr, function(item){
if(item.name.indexOf(s) > -1){
result.push(item);
}
});
return result;
}

Thanks to all again