Easy Ways to Loop Over Objects in JavaScript

  Рет қаралды 7,460

dcode

dcode

8 ай бұрын

In today's video, we have a look at some useful JavaScript object functions that let you iterate over an object's keys, values or both.
For your reference, check this out:
developer.mozilla.org/en-US/d...
🏫 My Udemy Courses - www.udemy.com/user/domenic-co...
🎨 Download my VS Code theme - marketplace.visualstudio.com/...
💜 Join my Discord Server - / discord
🐦 Find me on Twitter - / dcodeyt
💸 Support me on Patreon - / dcode
📰 Follow me on DEV Community - dev.to/dcodeyt
📹 Join this channel to get access to perks - / @dcode-software
If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!
#dcode #javascript

Пікірлер: 8
@montebont
@montebont 5 күн бұрын
Nice one- Thanks for sharing. To be more precise these are static functions of Object. You get all of them for free for _every instance_ of Object. The syntax may seem weird but it is very useful. You might use this when you want to group common helper functions like rounding numbers. Just declare them as static functions. The example below might be helpful because _proper_ rounding in JS is not as easy as it seems 🙂 You'd typically use it as ML.round(1 / 3, 5) class ML { /** * * @param {float} number * @param {int} precision * @returns (number) rounded number */ static round(number, precision) { const power = 10 ** precision; let result = Math.round((number * power).toPrecision(15)) / power; return result; } static roundUp(number, precision) { const power = 10 ** precision; let result = Math.ceil((number * power).toPrecision(15)) / power; return result; } static roundDown(number, precision) { const power = 10 ** precision; let result = Math.floor((number * power).toPrecision(15)) / power; return result; } static RND2(number) { return this.round(number, 2); }
@N1rOx
@N1rOx 7 ай бұрын
Thanks for the video! also thanks for the command/ctrl + D trick to select all. Have been wondering how people did that but haven't been bothered to learn just yet.
@Cobitremolo
@Cobitremolo 5 ай бұрын
I love it. You can do lots of things with those methods. Great video.
@brandonklevans7473
@brandonklevans7473 7 ай бұрын
Hey good video. I am also trying to convert data from a text file. An example of matching text in the text file would be a string that equals "TPER2" that needs to read "2TPER". Any idea how I can make a function that will take a string (containing letters followed by a number on the right) as a parameter and move the number to the left of the letters. Any guidance?
@patrickgomes2261
@patrickgomes2261 8 ай бұрын
What do you use to have this cursor effect on vscode?
@Sayvai
@Sayvai 8 ай бұрын
So with passing an array within an object into "new URLSearchParams()", it's entirely ok, as that's what i just discovered. JavaScript will properly encode the array items within the stringified URL params. And converting back to a two-deimensional array is relatively simple, although the it will no longer be an inner array value of hobbies, rather, just a comma delimited string of hobbies. See code snippet example below: ``` const obj = { id: "123", hobbies: ["football", "formula 1", "nutrition"], age: 36, location: "London", }; const entries = Object.entries(obj); const urlParams = new URLSearchParams(entries); const stringifiedUrlParams = urlParams.toString(); console.log("Stringified URL Params", stringifiedUrlParams); /** Convert from query string back to two-dimensional array **/ // Parse query string into URLSearchParams object const urlParams2 = new URLSearchParams(stringifiedUrlParams); const urlParamsEntries = urlParams2.entries(); // urlParamsEntries is now an iterable URLSearchParams Iterator // Convert URLSearchParams iterable object `urlParamsEntries` to two-dimensional array const twoDimensionalArray = Object.entries(Object.fromEntries(urlParamsEntries)); // 💡if you want to only convert back to an object, then there's no need to wrap with "Object.entries()" console.log(twoDimensionalArray); // Output: [["id", "123"], ["hobbies", "football,formula 1,nutrition"], ["age", "36"], ["location", "London"]] ```
@montebont
@montebont 5 күн бұрын
That's a design choice...A common use case is to use JSON to transfer data between client and server to preserve the key-value relations. But JSON is very portable but not very efficient for large datasets. Another approach could be (but your mileage may vary) to transfer the first row of the data as headers and reconstruct the object in JS when the download has finished. It all depends on the size, the latency of your web server and the download speed of the client's network.
@lounes03b99
@lounes03b99 8 ай бұрын
thanks its heplful video 📹 do u have a playlist of the step after [HTML, CSS, JS] , i'm a beginner and idon't know what should i do ??? have you an instagram account? and thank you again for your efforts
Maps vs. Objects in JavaScript - What's the Difference?
11:48
Common JavaScript Mistakes and How to Avoid Them
18:48
dcode
Рет қаралды 6 М.
Китайка и Пчелка 4 серия😂😆
00:19
KITAYKA
Рет қаралды 3,6 МЛН
Как быстро замутить ЭлектроСамокат
00:59
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 13 МЛН
Balloon Stepping Challenge: Barry Policeman Vs  Herobrine and His Friends
00:28
How to iterate through a JavaScript object literal
9:17
OpenJavaScript
Рет қаралды 8 М.
Javascript Objects Explained | Javascript Objects Tutorial
23:17
All useEffect Mistakes Every Junior React Developer Makes
22:23
Understanding Passing by Reference or Value in JavaScript
13:16
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 6 М.
3+ Ways to Write Clean Code in JavaScript
13:33
dcode
Рет қаралды 9 М.
5 POWERFUL JavaScript Events You Didn't Know
10:43
dcode
Рет қаралды 15 М.
How to Loop Over Objects in JavaScript | Objects | JavaScript
14:58
Китайка и Пчелка 4 серия😂😆
00:19
KITAYKA
Рет қаралды 3,6 МЛН