It would be nice if we could see whether an element is present in an array using the of operator.
like: 3 of [5, 4, 3, 2, 1] // true
there's of course the Array.prototype.includes() method but the new semantics to of
seems to be more intuitive.
1 Like
This doesn't appear very intuitive to me. I'd expect 3 of x
to be equivalent to x.slice(0, 3)
or x[Symbol.iterator]().take(3)
.
I could understand using 3 in x
like Python does it, unfortunately that already has (different) semantics in JS. After all, x.includes(3)
is very unambiguous.
I'd rather see someone add .includes
to https://github.com/tc39/proposal-iterator-helpers than this syntax.
Though now that I think about it, a in b
+ for (let a in b)
does provide some very strong precedent for this - a in b
is basically GetEnumerableProperties(b).includes(a)
(except it bails out early), and so desugaring like this might actually be a good idea:
// Source
const result = a of b
// Implementation
function _binaryOf(a, b) {
for (const item of b) if (SameValueZero(item, a)) return true
return false
}
const result = _binaryOf(a, b)
It's an open question how to handle async iterables syntactically, though.