⊗jsPmLpOCh 162 of 502 menu

Changing objects with a loop in JavaScript

Objects can also be changed in a loop. Let, for example, we have such an object:

let obj = {a: 1, b: 2, c: 3};

Let's loop through it with for-in and increase each of its elements by 2 times:

for (let key in obj) { obj[key] = obj[key] * 2; }

Let's check the result:

console.log(obj);

Given an object:

let obj = {x: 1, y: 2, z: 3};

Loop through this object and square each element of this object.

Given an object:

let obj = {x: 1, y: 2, z: 3};

Iterate over this object and increment each element of this object by one.

enru