Suggestion: a chainable in-place alternative of `Array.prototype.map`.
# Naveen Chawla (7 years ago)
Already discussed (at the end there's an Array.prototype.each idea that's the same): esdiscuss.org/topic/return
Already discussed (at the end there's an Array.prototype.each idea that's the same): https://esdiscuss.org/topic/return-value-of-foreach On Thu, 8 Feb 2018 at 14:17 Yeong-u Kim <wlzla000 at naver.com> wrote: > # Suggestion: a chainable in-place alternative of `Array.prototype.map`. > > > > ---------- > > > > I personally do make massive use of `Array.prototype.map` whenever I code > in ECMAScript. However, it often leaves an array waiting for garbage > collection (which makes unnecessary memory use and burdens the garbage > collector) when you no longer need the original array values. In order to > avoid it, you may do this in-place with `Array.prototype.forEach` instead. > But the problem is that it is not chainable unlike `Array.prototype.fill`. > > > > ```javascript > > /// To get ["#apple", "#banana", "#cabbage"] > > > > const tag = name => ('#' + name); > > > > if("Try #1") > > { > > ["apple", "banana", "cabbage"].forEach((n, i, a) => { > > a[i] = tag(n); > > }); // It is not chainable -- returns nothing but `undefined`. > > } > > > > if("Try #2") > > { > > // So we need to declare a variable holds the array in this scope. > > const array = ["apple", "banana", "cabbage"]; > > array.forEach((n, i) => { > > array[i] = tag(n); > > }); // We cannot feed it `tag` directly. > > const result = array; > > } > > > > if("Try #3") > > { > > const result = ["apple", "banana", "cabbage"].map(tag); > > // It looks neat, but does not make use of the existing memory and leaves > ‘garbage.’ > > } > > > > if("Suggestion") > > { > > const result = ["apple", "banana", "cabbage"].update(tag); > > // The array values are modified (updated) by `tag` in-place, > > // and it returns the updated array -- it's chainable. > > } > > ``` > > > > So I suggest `Array.prototype.update` and `TypedArray.prototype.update`. > It is similar to `Array.prototype.map` except for its in-place > modification. It is also similar to `Array.prototype.fill`. > > > > ```javascript > > Array.prototype.update(function callback(currentValue[, index[, array]]) > {......}[, thisArg]) > > ``` > > > > | | fill | map > | update | > > > |-------------------------------------------------------------|:----:|:---:|:------:| > > | Chainable | Yes | Yes > | Yes | > > | In-place modification (a side effect on the original array) | Yes | No > | Yes | > > | Returns a reference to the input (original) array. | Yes | No > | Yes | > > | Ignores holes (empty elements). | No | Yes > | Yes | > > | Makes use of the input (original) array values. | No | Yes > | Yes | > > > > > > How do you think about this suggestion? > > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180208/7b1d2679/attachment.html>
Suggestion: a chainable in-place alternative of
Array.prototype.map
.I personally do make massive use of
Array.prototype.map
whenever I code in ECMAScript. However, it often leaves an array waiting for garbage collection (which makes unnecessary memory use and burdens the garbage collector) when you no longer need the original array values. In order to avoid it, you may do this in-place withArray.prototype.forEach
instead. But the problem is that it is not chainable unlikeArray.prototype.fill
./// To get ["#apple", "#banana", "#cabbage"] const tag = name => ('#' + name); if("Try #1") { ["apple", "banana", "cabbage"].forEach((n, i, a) => { a[i] = tag(n); }); // It is not chainable -- returns nothing but `undefined`. } if("Try #2") { // So we need to declare a variable holds the array in this scope. const array = ["apple", "banana", "cabbage"]; array.forEach((n, i) => { array[i] = tag(n); }); // We cannot feed it `tag` directly. const result = array; } if("Try #3") { const result = ["apple", "banana", "cabbage"].map(tag); // It looks neat, but does not make use of the existing memory and leaves ‘garbage.’ } if("Suggestion") { const result = ["apple", "banana", "cabbage"].update(tag); // The array values are modified (updated) by `tag` in-place, // and it returns the updated array -- it's chainable. }
So I suggest
Array.prototype.update
andTypedArray.prototype.update
. It is similar toArray.prototype.map
except for its in-place modification. It is also similar toArray.prototype.fill
.Array.prototype.update(function callback(currentValue[, index[, array]]) {......}[, thisArg])
fill
map
update
How do you think about this suggestion?