繁体   English   中英

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

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

我正在尝试使用for...in循环遍历 object 以获取其所有属性的值并将它们放入变量中。 我绊倒了这一点。

我想做的是循环遍历 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
})

你试过 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
})

您可以直接从响应中访问属性:

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