0

I'm using the following a lot in my code. Can I do it as a one liner?

function anotherFunction(array)
{
    new_array = [];
    for (var i = 0; i < array.length; i++) 
    {
        new_array.push(someFunction(array[i]));
    }  
    return new_array;
}
2
  • Yeah, remove the newline's ;) just kidding. Commented Aug 6, 2013 at 14:03
  • array.push should be new_array.push and return array should be return new_array, I think Commented Aug 6, 2013 at 14:03

3 Answers 3

6

What you want is to "map" the array over a function:

var arr  = [...];
var arr2 = arr.map(someFunction);
Sign up to request clarification or add additional context in comments.

7 Comments

A note that Array.prototype.map is ES5 and not Compatible with older browsers would be nice though =)
I think this may not be supported in IE8. I think it could be pretty easily tacked onto the array prototype; I just need to recall if doing that would have some sort of caveat.
The link to the documentation provides a backwards compatibility shim and the much used ES5Shim compatibility library includes it IIRC.
@C5H8NNaO4 and ben Since it can be implemented trivially in user code, that is pretty irrelevant. A more correct thing to say is that it doesn't work out of the box in IE8. Stuff like File IO is truly unsupported in IE8 and should be differentiated.
|
1
var newArray = array.map(someFunction);

You should note that this may fail on IE8 if you need to support that though

http://kangax.github.io/es5-compat-table/#Array.prototype.map

Comments

0

if you also want it fast... Array.prototype.map is not so fast as a loop.

http://jsperf.com/array-map-vs-loop/2

so:

function customMap(a,c){
 var b=[],l=a.length;
 while(l--){
  b[l]=c(a[l]);
 }
 return b;
}

and return it with

var newArray=customMap(oldArray,function);

this is very fast.

you can also create a custom prototype.

Object.defineProperty(Array.prototype,'CMap',{value:function(c){
 var b=[],l=this.length;while(l--){b[l]=c(this[l])}
 return b;
},writable:false,enumerable:false});

and use it like map.

var newArray=oldArray.CMap(function);

EDIT

here is the test on jsperf...

http://jsperf.com/custom-vs-map

every variable is defined outside the test... so speed is based only on the custom function.

and this works on all browsers.

6 Comments

Variable name length doesn't affect performance so you didn't relaly have to minify it :P But yes, Array.prototype.map involves far more convoluted semantics than a common sense loop so it's slower.
nah... i really tested ... i just got the first link of jsperf... push vs direct setting && map vs custom function are slower... also the while-- loop is faster in real world. you can't even test the while -- loop in jsperf.
almost there, don't put code in setup and do work in the function jsperf.com/custom-vs-map/2
yea jsperf is actually really hard (or well, unintuitive) to use if you want correct results
so this is 6-7 times faster than the normal map.. and it's compatible with prolly all browsers with javascript support.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.