简体   繁体   English

遍历 object 属性并将值放入 javascript 中的变量中

[英]Iterating through and object property and putting the values inside variables in javascript

I'm trying to iterate through an object using the for...in loop to get the values of all its properties and put them inside variables.我正在尝试使用for...in循环遍历 object 以获取其所有属性的值并将它们放入变量中。 I'm stumbling on this.我绊倒了这一点。

What I would like to do is loop through the object properties, put the value inside a variable of the same the name as the property to use on them on another variable我想做的是循环遍历 object 属性,将value放在与要在另一个变量上使用的property同名的变量中


somePromise.then(response => {  

  for (let property in response) {
    property = response[property]  // this doesn't work cause reuses the same variable
  }

  /// I take the value of each property from the loop and pass it down 
  user.updateInfoFromOldDB(
    userID,
    nguid,
    property1 
    property2,
    property3
    property4,
  )

  ...ommited_code
})

Did you try Object.values()?你试过 Object.values() 吗?

somePromise.then(response => {  

  const valuesArr = Object.values(response);

  /// I take the value of each property from the loop and pass it down 
  user.updateInfoFromOldDB(
    ...valuesArr
  )

  ...ommited_code
})

You can directly access the properties from the response:您可以直接从响应中访问属性:

somePromise.then(response => {  
  user.updateInfoFromOldDB(
    response.userID,
    response.nguid,
    response.property1,
    response.property2,
    response.property3
    response.property4,
  )
})

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

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