Hi,

I'm just wondering why there is no mutable function with Javascript arrays.

For exemple, if I want to remove every entry matching 42 from an array, I
will have to write something like :

> var array = [ 0, 1, 42, 3, 4, 5, 42, 42, 42 ];
> array = array.filter( function ( x ) { return x === 42; } );

Why is it necessary to make a copy ? Why can't we just write something like
this ?

> var array = [ 0, 1, 42, 3, 4, 5, 42, 42, 42 ];
> array.mfilter( function ( x ) { return x === 42; } ); // mutable filter

The alternative is to use a for loop with splice, but some optimisations
won't be available (removing multiple elements at the time ) :

> var array = [ 0, 1, 42, 3, 4, 5, 42, 42, 42 ];
> for ( var t = array.length; -- t; ) if ( array[ t ] === 42 )
array.splice( t, 1 );

Same for map :

> var array = [ 0, 1, 42, 3, 4, 5, 42, 42, 42 ];
> array.mmap( function ( ) { return 42; } ); // mutable map

The alternative would be to use the forEach method using three arguments :

> var array = [ 0, 1, 42, 3, 4, 5, 42, 42, 42 ];
> array.forEach( function ( x, i, a ) { a[ i ] = 42; } );

Is there a reason for these missing functions ?

Thanks,

-- 
Maël Nison
Epitech 2014, Paris - http://www.arcastel.com
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to