简体   繁体   English

如何使用数组从 JavaScript 转换为 Python

[英]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.我正在翻译业务合作伙伴写给 Python 的一些 JavaScript 代码。 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.我对 JavaScript 有点熟悉,对 Python 非常不熟悉,但在遇到一些我以前在 Z686155AF75A60A0F6E9D80C1FZEDD 中从未见过的代码之前,我一直取得了不错的进展。

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.我知道 for of 根据 mdn 迭代可迭代对象,但我之前没有看到使用数组完成它,如此处所示。 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.我希望有人能澄清那条线在做什么,也许还有一个如何以pythonic方式做到这一点的例子。

Have a great day!祝你有美好的一天!

You are looking at a Destructuring assignment .您正在查看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.发生的事情是errors是 arrays 的数组,每个元素的大小为 2。在每个循环迭代中,您获取该元素,而不是在其上调用element[0]element[1] ,您已经赋予它意义名字。

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

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