-
20 Mar 2012 11:06 AM #1
Unanswered: Select from array, is it possible?
Unanswered: Select from array, is it possible?
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
-
20 Mar 2012 11:14 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,582
- Vote Rating
- 434
- Answers
- 3102
You can use RegEx but you need something to look at the object. We have one for an array of strings.
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
20 Mar 2012 11:15 AM #3
What you're looking for, I think, is the function Ext.Array.filter which can be used like so:
Code:var pops = Ext.Array.filter(array, function(item) { return item.name.indexOf('Pop') != -1; });
-
20 Mar 2012 12:13 PM #4
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/#!...-method-filter
-
20 Mar 2012 1:16 PM #5
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


Reply With Quote