Results 1 to 5 of 5

Thread: Select from array, is it possible?

  1. #1
    Sencha User yuryn1961's Avatar
    Join Date
    Nov 2011
    Posts
    6
    Vote Rating
    0
      0  

    Default 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

  2. #2
    Sencha - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,553
    Answers
    3931
    Vote Rating
    1272
      0  

    Default

    You can use RegEx but you need something to look at the object. We have one for an array of strings.
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

  3. #3
    Ext JS Premium Member burnnat's Avatar
    Join Date
    Jun 2011
    Posts
    418
    Answers
    42
    Vote Rating
    64
      0  

    Default

    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;
    });

  4. #4
    Sencha Premium User
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    1,932
    Answers
    18
    Vote Rating
    548
      0  

    Default

    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

  5. #5
    Sencha User yuryn1961's Avatar
    Join Date
    Nov 2011
    Posts
    6
    Vote Rating
    0
      0  

    Default

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •