views:

480

answers:

3

I am so used to the syntactic sugar of C# (and not as used to javascript as I want to) that I find this very verbose.

Is there any better way to do this?

var justColumnNames = new Array();    
for( i= 0; i< columnsInfo.length; i++)
{
   justColumnNames[i] = columnsInfo[i].Name;
}

(BTW I have the Extjs available in the page, and I cant really use any other library) Thanks

+4  A: 
Ext.each( columnsInfo, function(elem){
    justColumnNames.push(elem.name);
});
Upper Stage
While this looks better, it is in effect just as verbose and arguably adds a layer of complexity by using a closure. If you add the `justColumnNames` definition, it isn't that much shorter.It is a bit cleaner though for people already used to Ext and its `each` method, or that of similar frameworks, and less can go wrong.
Kamiel Wanrooij
So, by your admission it is cleaner, looks better, and less can go wrong. I humbly suggest it meets the criteria requested. Nevertheless, your points are well taken.
Upper Stage
@Upper Stage - You're ignoring the requirement to use Ext. Another point to consider is that your version is pretty much certain to be slower than the original, though admittedly that's not always going to be an issue.
Tim Down
@Tim Down - This solution does not not ignore ExtJS, rather it exploits it as Ext.each() is a function/utility provided by ExtJS. Slower? Yes, but I understand the request is to reduce verbosity.
Upper Stage
@Upper Stage: you missed my point, which was that ExtJS is required by your answer, and that it is a heavy requirement just to use a simple utility function. However, I now see the question mentions that the OP has ExtJS available, so my point is null and void.
Tim Down
@Tim Down: I see; you were suggesting I used a big hammer (ExtJS) on a small problem. Had @Miau not had ExtJS available, I would agree with your point.
Upper Stage
+2  A: 

What you're looking for is a map() function, which takes the values in an array, applies a function to each value and returns an array containing the mapped values. I'm not familiar enough with ExtJS to know if it includes a map function by default, but this question links to some plugins you can use.

Once you have a map function available, you can do something like this:

justColumnNames = columnsInfo.map(function(elem) { elem.Name });

Martin Gordon
A: 

You can easily add syntactic sugar in javascript. One common one is to implement a foreach/map/filter method for arrays. Most libraries do this. My implementation:

// List object, inherits from Array
function List (array) {
    // Allow List constructor to convert
    // Array object into List object:
    if (array !== undefined) {
        this.push.apply(this,array)
    }

    // each() method. A cross between map and filter:
    this.each = function (callback) {
        var ret = new List();
        for (i=0,l=this.length;i<l;i++) {
            var r = callback(this[i],i);
            if (r !== undefined) {
                ret.push(r);
            }
        }
        return ret;
    }
}
List.prototype = new Array();

// Direct translation of your code:
var justColumnNames = new List();
justColumnNames.each(function(n,i){
   n = columnsInfo[i].Name;
});

// Or the more brief:
var justColumnNames = new List(columnsInfo).each(function(n){return n.Name});

Some people modify the Array constructor directly by doing:

Array.prototype.each = function (callback) {
        // see implementation above ...
}

But I generally don't like modifying native objects.

slebetman