Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array.push return pushed value?

Are there any substantial reasons why modifying Array.push() to return the object pushed rather than the length of the new array might be a bad idea?

I don't know if this has already been proposed or asked before; Google searches returned only a myriad number of questions related to the current functionality of Array.push().

Here's an example implementation of this functionality, feel free to correct it:

;(function() {
    var _push = Array.prototype.push;
    Array.prototype.push = function() {
        return this[_push.apply(this, arguments) - 1];
    }
}());

You would then be able to do something like this:

var someArray = [],
    value = "hello world";

function someFunction(value, obj) {
    obj["someKey"] = value;
}

someFunction(value, someArray.push({}));

Where someFunction modifies the object passed in as the second parameter, for example. Now the contents of someArray are [{"someKey": "hello world"}].

Are there any drawbacks to this approach?

like image 246
Elliot Bonneville Avatar asked Apr 29 '14 03:04

Elliot Bonneville


People also ask

Does Push return the array?

push() The push() method adds one or more elements to the end of an array and returns the new length of the array.

Does Push return a value?

The push() method returns the new value of the length property of the array object on which you call the method.

What does array push return in JavaScript?

JavaScript Array push() The push() method returns the new length.

What does push () method of the array do?

push() method is used to push one or more values into the array. This method changes the length of the array by the number of elements added to the array. Parameters This method contains as many numbers of parameters as the number of elements to be inserted into the array.


3 Answers

See my detailed answer here

TLDR;
You can get the return value of the mutated array, when you instead add an element using array.concat[].

concat is a way of "adding" or "joining" two arrays together. The awesome thing about this method, is that it has a return value of the resultant array, so it can be chained.

newArray = oldArray.concat[newItem];

This also allows you to chain functions together

updatedArray = oldArray.filter((item) => { item.id !== updatedItem.id).concat[updatedItem]};

Where item = {id: someID, value: someUpdatedValue}

The main thing to notice is, that you need to pass an array to concat.
So make sure that you put your value to be "pushed" inside a couple of square brackets, and you're good to go.
This will give you the functionality you expected from push()

You can use the + operator to "add" two arrays together, or by passing the arrays to join as parameters to concat().

let arrayAB = arrayA + arrayB;
let arrayCD = concat(arrayC, arrayD);

Note that by using the concat method, you can take advantage of "chaining" commands before and after concat.

like image 98
SherylHohman Avatar answered Oct 14 '22 11:10

SherylHohman


Are there any substantial reasons why modifying Array.push() to return the object pushed rather than the length of the new array might be a bad idea?

Of course there is one: Other code will expect Array::push to behave as defined in the specification, i.e. to return the new length. And other developers will find your code incomprehensible if you did redefine builtin functions to behave unexpectedly.

At least choose a different name for the method.

You would then be able to do something like this: someFunction(value, someArray.push({}));

Uh, what? Yeah, my second point already strikes :-)

However, even if you didn't use push this does not get across what you want to do. The composition that you should express is "add an object which consist of a key and a value to an array". With a more functional style, let someFunction return this object, and you can write

var someArray = [],
    value = "hello world";

function someFunction(value, obj) {
    obj["someKey"] = value;
    return obj;
}

someArray.push(someFunction(value, {}));
like image 21
Bergi Avatar answered Oct 14 '22 11:10

Bergi


Just as a historical note -- There was an older version of JavaScript -- JavaScript version 1.2 -- that handled a number of array functions quite differently.

In particular to this question, Array.push did return the item, not the length of the array.

That said, 1.2 has been not been used for decades now -- but some very old references might still refer to this behavior.

http://web.archive.org/web/20010408055419/developer.netscape.com/docs/manuals/communicator/jsguide/js1_2.htm

like image 24
Jeremy J Starcher Avatar answered Oct 14 '22 09:10

Jeremy J Starcher