I recently got tricked by calling Object.keys()
/ Object.values()
/ Object.entries()
on a Map
object, and it took me a while to realize that it was wrong:
const m = new Map();
m.set('a', 1);
m.set('b', 2);
console.log('m.size:', m.size);
// Incorrect:
console.log('Object.keys(m):', Object.keys(m)); // Array [] (empty)
console.log('Object.values(m):', Object.values(m)); // Array [] (empty)
console.log('Object.entries(m):', Object.entries(m)); // Array [] (empty)
// Correct:
console.log('Array.from(m.keys()):', Array.from(m.keys())); // Array [ "a", "b" ]
console.log('Array.from(m.values()):', Array.from(m.values())); // Array [ 1, 2 ]
console.log('Array.from(m.entries()):', Array.from(m.entries())); // Array [ Array [ "a", 1 ], Array [ "b", 2 ] ]
Would you be open to add a rule detecting the incorrect usage above?
var m = new Map();
m.set('a', 1);
m.set('b', 2);
console.log(Object.keys(m));
var m = new Map();
m.set('a', 1);
m.set('b', 2);
console.log(Object.values(m));
var m = new Map();
m.set('a', 1);
m.set('b', 2);
console.log(Object.entries(m));
var m = new Map();
m.set('a', 1);
m.set('b', 2);
console.log(Array.from(m.keys()));
var m = new Map();
m.set('a', 1);
m.set('b', 2);
console.log(Array.from(m.values()));
var m = new Map();
m.set('a', 1);
m.set('b', 2);
console.log(Array.from(m.entries()));
No response
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too