Mots clés : javascriptarraysalgorithmtime-complexityjavascript-objectsjavascript
93
console.log(['joe', 'jane', 'mary'].includes('jane')); //true
console.log(['joe', 'jane', 'mary'].indexOf('jane') >= 0); //true
82
function contains(a, obj) { var i = a.length; while (i--) { if (a[i] === obj) { return true; } } return false; }
Array.prototype.contains = function(obj) { var i = this.length; while (i--) { if (this[i] === obj) { return true; } } return false; }
alert([1, 2, 3].contains(2)); // => true alert([1, 2, 3].contains('2')); // => false
72
[1, 2, 3].indexOf(1) => 0 ["foo", "bar", "baz"].indexOf("bar") => 1 [1, 2, 3].indexOf(4) => -1
66
[1, 2, 3].includes(2); // true [1, 2, 3].includes(4); // false
[1, 2, 3].includes(3, 3); // false [1, 2, 3].includes(3, -1); // true
[1, 2, NaN].includes(NaN); // true
new Array(5).includes(undefined); // true
57
const items = [ {a: '1'}, {a: '2'}, {a: '3'} ] items.some(item => item.a === '3') // returns true items.some(item => item.a === '4') // returns false
if (items.some(item => item.a === '3')) { // do something }