简体   繁体   中英

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. 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


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()?

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,
  )
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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