Here is what I'm doing:
function getIndexes(n) {
return n
.toString(2)
.split("")
.map((c, i) => [c, i])
.filter(([c, _]) => c == "1")
.map(([_, i]) => i)
;
}
console.log(getIndexes(6));
console.log(getIndexes(7));
console.log(getIndexes(42));
Explanation:
[character, index] tuple"1"It feels a bit of an overkill.
Is there a more standard way to achieve that?
I care less about performance and more about using something cleaner and/or custom.
Thanks!
If I well understood what you want to achieve, here is an alternative solution:
function getIndexes(n) {
const result = [];
const long_n = BigInt(n);
for(let i = 0n; i < 64n; ++i) {
if((long_n >> i) & 1n) result.push(Number(i));
}
return result;
}