admin管理员组文章数量:1279208
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);
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);
Share
Improve this question
asked Nov 20, 2018 at 16:18
user2669997user2669997
2
- 4 entries is a different object than temps. – Fallenreaper Commented Nov 20, 2018 at 16:21
- You already have the solution. You are changing values of "entries" and printing value of "temps". Instead of temps in the end use entries. – Saurabh Sarathe Commented Nov 20, 2018 at 16:23
5 Answers
Reset to default 6You 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);
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)
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);
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);
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 ments, 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;
}
本文标签: javascriptloop through object array and change the valuesStack Overflow
版权声明:本文标题:javascript - loop through object array and change the values - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741268164a2368862.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论