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

2
  • I think you have the best way. LINQ's too cool for JavaScript. Commented Jan 15, 2010 at 14:57
  • Doug, where'd your answer go?? Commented Jan 15, 2010 at 15:04

3 Answers 3

5
Ext.each( columnsInfo, function(elem){
    justColumnNames.push(elem.name);
});
Sign up to request clarification or add additional context in comments.

6 Comments

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.
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 - 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 - 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: 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.
|
2

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

Comments

-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.