sfl*_*che 3 javascript arrays lodash
我正在使用 lodashincludes函数来检查数组中是否存在目标值......
_.includes(array, target)
Run Code Online (Sandbox Code Playgroud)
并希望在 ES5(或 ES6)中找到一个很好的等价物
我错过了什么?ES5 Array.prototype 没有等效的吗?
或者是我唯一可以使用的选择indexOf?
ES7: Array.prototype.includes()
[1, 2, 3].includes(2); // true
Run Code Online (Sandbox Code Playgroud)
ES5: Array.prototype.indexOf()>= 0
[2, 5, 9].indexOf(5) >= 0; // true
[2, 5, 9].indexOf(7) >= 0; // false
Run Code Online (Sandbox Code Playgroud)
如果您更喜欢函数:
function includes (array, element) { return array.indexOf(element) >= 0 }
Run Code Online (Sandbox Code Playgroud)
并将其用作:
includes([2, 5, 9], 5); // true
Run Code Online (Sandbox Code Playgroud)