DEV Community

loading...

Discussion on: 17 Pro JavaScript tricks you didn't know

Collapse
thr0tt1e profile image
Thr0TT1e • Edited

ERROR - "Object to Array"

// Noobs:

let number = {
    one: 1,
    two: 2,
};
let keys = [];
for (let numbers in number) {
    if (number.hasOwnProperty(numbers)) {
        keys.push(numbers);
    }
}

console.log(keys) // [ 'one', 'two' ]
Enter fullscreen mode Exit fullscreen mode

// Pro:

let number = {
    one: 1,
    two: 2,
};
let key   = Object.keys(number) // [ 'one', 'two' ]
let value = Object.values(number) // [ 1, 2 ]
let entry = Object.entries(number) // [ [ 'one', 1 ], [ 'two', 2 ] ]
Enter fullscreen mode Exit fullscreen mode
Collapse
rahxuls profile image
Rahul Author

Everyone has their own perspective sir.

Collapse
mcstepp profile image
Meg Stepp

No he meant your had typos in your variable names. You declared a "number" variable but referenced "numbers" (with an s) in your code examples. The code literally doesn't run.

Thread Thread
rahxuls profile image