How to loop over each key value pair in a javascript object #shorts

preview_player
Показать описание
My VSCode Extensions:
- theme: material community high contrast
- fonts: Menlo, Monaco, 'Courier New', monospace
- errors: Error Lens
- extra git help: Git Lens
- tailwind css intellisense
- indent rainbow
- material icon theme
- prettier & eslint
- ES7+ React Snippets

------------

Рекомендации по теме
Комментарии
Автор

keep these shorts coming bro, they are really helpful

theofanisbirmpilis
Автор

there is also Object.keys and Object.values

backiscute
Автор

This is indeed really good man. Keep it up.

oussamaboumaad
Автор

One thing to note, this is O(2N). Because you loop 2 times (for of and Object.entries)

Sometimes i just use
for(const key in object){
const value = object[key]
}

z-aru
Автор

I usually dont comment, but honestly this is the best thing on the internet! Keep it up!

TheRealSlimOcha
Автор

I think you can use for in loop to loop over that object

gamingwithrk
Автор

Thanks man. Really helpful. Can you do a short on destructuring ?

Paul_Aderoju
Автор

You can even map the object like array by this
(Object.entry).map()=>

masoudnaji
Автор

I've been always reminded of using for loop than forEach for better performance, but it's just easier to read. I usually do => { ... }).

adet
Автор

Nice. Mostly I use Object.keys() to access the keys of object. inside the loop I use the key as an index of the object to get its value.

davepascual
Автор

im not learning js at the moment but theese could come in helpful while im snooping around on website's inspect element thingy

thesexman
Автор

Variable in (for-of) loop takes the whole object at once, no need for . entries....We can though use in traditional for loop

Sartaj_Ashraf
Автор

Instead of 'let pair' you can do 'let [key, value]' to have them in two different variables

boem
Автор

Or you do `Object.entries(array).forEach(([key, value]) => {
/// do sth
});


Which is - in my optinion - a bit more readable.

YamiSuzume
Автор

It'd be nice if they provide a shortcut that returns as a generator rather than as a list

NoProblem
Автор

in my opinion, just using for...in would be more concise and avoids extra allocation.
Alternatively you can use Object.entries(obj).map(([key, value])=>{}) method.

Yutaro-Yoshii
Автор

Object.entries, destructuring while using an off were takeaways for me. I did know about Object.keys but i know better when to use Object.entries.

ajithmoni
Автор

You should be able to do that for loop without object.entries(). I think…

IkeVictor
Автор

what should i use in object to loop "in" or "of".?

aliensoul
Автор

What about Object.entries(hasPaid).forEach((key, value) => {
console.log(key);
console.log(value);
});?

May have errors in it as I have written this comment on my phone. It's shorter and does the same thing as a for loop. :)

filipgornik