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.