Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array.prototype.find, what's its spec?

Tags:

javascript

Those who have Chrome 30.0.1599.14 dev seem to be gifted of this new function:

String(Array.prototype.find); // "function find() { [native code] }"

However, I haven't found any reference for this addition. From my tests it seems that it accepts arguments a-la some, every and so on:

array.some(callback[, thisObject]);

and callback is the usual callback function of that kind:

function([item[, index[, array]]]) {
   ...
}

The function is expected to return true when the "right" item is passed, so that item becomes the value returned by find. For example:

Array.prototype.slice.call(document.getElementsByTagName("*"))
        .find(function(element) {return element.id === "content";});

It's an odd way to re-define document.getElementById, except when the item isn't found find returns undefined instead of null.

It could be nice if this function could accept an initial index, or if there could be a dual function like lastFind that parses the array from the end.

Is there anybody who's already seen this function and can tell me where I can find its spec?

like image 875
MaxArt Avatar asked May 02 '26 17:05

MaxArt


1 Answers

It is ECMA Script 6. Here's an HTML version of the ES6 spec. Here's the specific section for Array.prototype.find().

Update: also, the Chromium bug for Array.prototype.find implementation.

like image 140
kamituel Avatar answered May 05 '26 17:05

kamituel