简体   繁体   中英

How to translate for of with array from JavaScript to Python

I am working on translating some JavaScript code that a business partner wrote to Python. I am somewhat familiar with JavaScript and very unfamiliar with Python, but have been making good progress until I ran across some code that I haven't seen before in JavaScript.

let recordTypesSet = new Set();
for (let [errorType, accountList] of errors) {
  for (let i = 0; i < accountList.length; i = i + 1) {
    let error = accountList[i];
    recordTypesSet.add(error.recordType);
  }
}

I am not sure what the second line is doing really. I know that for of iterates over iterable objects as per mdn, but I haven't seen it done before with an array as is shown here. I was hoping someone could clarify what that line is doing and maybe an example of how to do that the pythonic way as well.

Have a great day!

You are looking at a Destructuring assignment . What is happening is that errors is an array of arrays, and each element is of size 2. In each for cycle iteration you take that element and instead of calling element[0] and element[1] on it, you already give it meaningful names.

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