簡體   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