1. #1
    Sencha User
    Join Date
    May 2008
    Posts
    180
    Vote Rating
    0
    archmisha is on a distinguished road

      0  

    Default Ext.pluck Improved !

    I suggest the following improvement for the pluck function:
    I am using it to extract ids from records list, giving prop = 'data.id'
    Code:
    Ext.pluck = function(arr, prop)
    {
        var oldPluck = function(arr, prop)
        {
            var ret = [];
            Ext.each(arr, function(v)
            {
                ret.push(v[prop]);
            });
            return ret;
        };
    
        var res = arr;
        Ext.each(prop.split('.'), function(v)
        {
            res = oldPluck(res, v);
        });
        return res;
    };

  2. #2
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    118
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default

    You can do that faster and with less code:
    Code:
    Ext.pluck = function(arr, prop){
        var ret = [], fn = Ext.data.JsonReader.prototype.getJsonAccessor(prop);
        Ext.each(arr, function(v){
            ret.push(fn(v));
        };
        return res;
    };

  3. #3
    Sencha User
    Join Date
    May 2008
    Posts
    180
    Vote Rating
    0
    archmisha is on a distinguished road

      0  

    Default

    Thanks

  4. #4
    Sencha User twisted_pear's Avatar
    Join Date
    Aug 2010
    Location
    North Carolina
    Posts
    23
    Vote Rating
    1
    twisted_pear is on a distinguished road

      0  

    Exclamation Close, but keine cigar

    Oops, missed a ) in there.

    Code:
    // extend Pluck to search down using '.' operator
    Ext.pluck = function(arr, prop){
        var ret = [], fn = Ext.data.JsonReader.prototype.getJsonAccessor(prop);
        Ext.each(arr, function(v){
            ret.push(fn(v));
        });
        return res;
    };
    That being said, it still does not return anything for me when it is overridden this way.
    var arr = { person : { name : { first : 'Jimbo', last : 'Jones' } } };
    i.e. Ext.pluck( arr , 'person' );
    i.e. Ext.pluck( arr , 'person.name' );


    Thanks

  5. #5
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,604
    Vote Rating
    58
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default

    "arr" is supposed to be an array, not an object.

  6. #6
    Sencha User twisted_pear's Avatar
    Join Date
    Aug 2010
    Location
    North Carolina
    Posts
    23
    Vote Rating
    1
    twisted_pear is on a distinguished road

      1  

    Lightbulb Pluck deep

    Here is a simple implementation to allow deep plucking. I did not want to use any sore of OGNL parsing, or a recursive function as the first post posits, simply due to stack space and speed. I don't think you'll need to go beyond a depth of 4. Thus, a simple switch should run fast enough, and not require a dirty eval().

    Code:
    /**
     * Normal Ext.pluck() only allows object depth of 1, so replace it
     * Maximum depth of pluck : 4
     * @param array values
     * @param string targe i.e. 'data.id'
     */
    Ext.pluck = function(arr, prop){
    	// how deep to go?
    	var parts = prop.split('.');
        var ret = [];
        switch( parts.length ) {
        	case 1:
            	// the old way
                Ext.each(arr, function(v) {
                    ret.push( v[prop] );
                });
        		break;
        	case 2:
                Ext.each(arr, function(v) {
                    ret.push( v[parts[0]][parts[1]] );
                });
        		break;
        	case 3:
                Ext.each(arr, function(v) {
                	ret.push( v[parts[0]][parts[1]][parts[2]] );
                });
        		break;
        	case 4:
                Ext.each(arr, function(v) {
                	ret.push( v[parts[0]][parts[1]][parts[2]][parts[3]] );
                });
        		break;
        	deault:
        		throw new Error('Maximum pluck depth of 4 exceded');
        }
        return ret;
    };

  7. #7
    Sencha Premium Member
    Join Date
    Sep 2015
    Location
    UK
    Posts
    20
    Vote Rating
    0
    rabia mahmood is on a distinguished road

      0  

    Default

    twisted_pear - My afternoon has exponentially improved because of your Ext.pluck improvement. Thank you!