user2669997
user2669997

Reputation:

loop through object array and change the values

i am trying to loop through an object array (not sure if that is correct terminoledgy) and want to change the values to each key within the list.

example if I have a list of room1 = 0, room2 = 0, room3 = 0 and after i have looped through it will be room1 = 10, room2 = 10, room3 = 10.

I can do it with an array but will then loose the keys which is very helpfull when using the array in other parts of my program.

I understand this has been asked before but cannot see a solution to changing the values

My code below changes the values within the loop but the console log outside the loop shows room2 is still equal 0

Any help please or is this something that cannot be done

temps = {room1:0, room2:0, room3:0};
const entries = Object.entries(temps);
for (i=0; i<3; i++){
  entries[i][1] = 10;
  console.log(entries[i]);
}
console.log("room2 = " + temps.room2);

Upvotes: 3

Views: 7450

Answers (5)

Mark
Mark

Reputation: 92461

You can just loop over the keys, you don't want Object.entries in this case because it's not giving you a reference into the original object:

let temps = {room1:0, room2:0, room3:0};
for (let key in temps) {
  temps[key] = 10
}
console.log(temps)
console.log("room2 = " + temps.room2);

Upvotes: 6

brk
brk

Reputation: 50346

Use for..in to loop through an array , but in this case create a new array which hold s the new values(if the new values are all different). Then use Object.keys to get all the keys from the object ,and then retrieve and update the value in the main object

const temps = {
  room1: 0,
  room2: 0,
  room3: 0
}

const newVals = [10, 20, 30]

Object.keys(temps).forEach((key, index) => {
  temps[key] = newVals[index]
})

console.log("room2 = " + temps.room2);

Upvotes: 1

epascarello
epascarello

Reputation: 207557

The problem you have is you are just altering the entries array and that has no relationship back to the parent. Changing it does not change the parent since it is just a copy of the keys and values, it is not a reference to them.

So you need to alter the actual object. You can just iterate over the keys and alter the object. forEach loop makes it easy.

const temps = {room1:0, room2:0, room3:0};
Object.keys(temps).forEach( function (key) {
  temps[key] = 10;
});
console.log(temps)

Upvotes: 2

Mr Alihoseiny
Mr Alihoseiny

Reputation: 1239

You can get an object keys with Object.keys. So the code is like it:

temps = {room1:0, room2:0, room3:0};
for (var key of Object.keys(temps)) {
    temps[key] = 10;
}

Object.keys(temps) returns an array of key values. As George mentioned in the comments, you can do it in a simpler way without using Objects.keys. For that, you can use in instead of of keyword in the for and pass the temps object itself. Here is the code:

temps = {room1:0, room2:0, room3:0};
for (var key in temps) {
    temps[key] = 10;
}

Upvotes: 0

Jim Wright
Jim Wright

Reputation: 6058

You can iterate over each key in the temps object, and update the temperature directly.

temps = {room1:0, room2:0, room3:0};
for (k in temps) {
  temps[k] = 10
}
console.log("room2 = " + temps.room2);

Upvotes: 0

Related Questions