简体   繁体   English

循环遍历对象数组并更改值

[英]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. 我试图循环一个对象数组(不确定这是否是正确的terminoledgy)并想要将值更改为列表中的每个键。

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. 例如,如果我有一个room1 = 0,room2 = 0,room3 = 0的列表,并且在我循环之后将是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 我的代码更改了循环中的值,但循环外的控制台日志显示room2仍然等于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); 

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: 您可以循环遍历键,在这种情况下您不需要Object.entries ,因为它没有为您提供对原始对象的引用:

 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. 您遇到的问题是您只是更改entries数组并且与父级没有任何关系。 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. forEach循环使它变得容易。

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

You can get an object keys with Object.keys . 您可以使用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. Object.keys(temps)返回键值数组。 As George mentioned in the comments, you can do it in a simpler way without using Objects.keys . 正如George在评论中提到的,您可以在不使用Objects.keys情况下以更简单的方式完成。 For that, you can use in instead of of keyword in the for and pass the temps object itself. 对于这一点,你可以用in代替of在关键字for并通过temps对象本身。 Here is the code: 这是代码:

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

You can iterate over each key in the temps object, and update the temperature directly. 您可以遍历temps对象中的每个键,并直接更新温度。

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

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). 使用for..in循环for..in数组,但在这种情况下,创建一个新数组,其中包含新值(如果新值全部不同)。 Then use Object.keys to get all the keys from the object ,and then retrieve and update the value in the main object 然后使用Object.keys从对象获取所有键,然后检索并更新主对象中的值

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM