Easy Ways to Loop Over Objects in JavaScript

preview_player
Показать описание
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:

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
Рекомендации по теме
Комментарии
Автор

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.

NrOx
Автор

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

montebont
Автор

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

Cobitremolo
Автор

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?

brandonklevans
Автор

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

patrickgomes
Автор

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

const urlParamsEntries = urlParams2.entries(); // urlParamsEntries is now an iterable URLSearchParams Iterator

// Convert URLSearchParams iterable object `urlParamsEntries` to two-dimensional array
const twoDimensionalArray = // 💡if you want to only convert back to an object, then there's no need to wrap with "Object.entries()"


// Output: [["id", "123"], ["hobbies", "football, formula 1, nutrition"], ["age", "36"], ["location", "London"]]
```

Sayvai
Автор

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

lounesb