简体   繁体   中英

'includes()' is for string, not for array in Javascript?

From: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object

Object.keys()
Returns an array containing the names of all of the given object's own enumerable string properties.

This says that 'includes' works on strings: https://www.w3schools.com/jsref/jsref_includes.asp

Then why is the following statement valid in the code shown in this SO thread?
https://stackoverflow.com/a/66758849/462608

const colorValues = Object.keys(this.colorValues);
const invalidColors = colors.filter(c => !colorValues.includes(c));

It exists on both prototypes: String.prototype.includes and Array.prototype.includes .

String.prototype.includes checks if the string contains the passed substring.

Array.prototype.includes checks if the array contains a particular value (with the SameValueZero algorithm).

Somewhat confusingly, they use the exact same name, but the logic employed is similar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM