Suggestion: a chainable in-place alternative of ``.

# Yeong-u Kim (17 days ago)

Suggestion: a chainable in-place alternative of

I personally do make massive use of 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.

/// 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.’
	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 except for its in-place modification. It is also similar to Array.prototype.fill.

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?

# Naveen Chawla (17 days ago)

Already discussed (at the end there's an Array.prototype.each idea that's the same):