Easy Ways to Loop Over Objects in JavaScript

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

  • @N1rOx
    @N1rOx7 ай бұрын

    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.

  • @montebont
    @montebont9 күн бұрын

    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); }

  • @Cobitremolo
    @Cobitremolo5 ай бұрын

    I love it. You can do lots of things with those methods. Great video.

  • @patrickgomes2261
    @patrickgomes22618 ай бұрын

    What do you use to have this cursor effect on vscode?

  • @brandonklevans7473
    @brandonklevans74737 ай бұрын

    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?

  • @Sayvai
    @Sayvai8 ай бұрын

    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

    9 күн бұрын

    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
    @lounes03b998 ай бұрын

    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