简体   繁体   English

如何在不使用 for 循环的情况下迭代 FormData entries() 数组

[英]How to iterate over a FormData entries() array without using for-loop

I try to iterate over a FormData entries() object.我尝试遍历 FormData 条目()对象。 ESlint is giving me error that I really shouldn't do it the way I have done below: iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations. ESlint 给了我一个错误,我真的不应该像下面那样做: iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations. iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations.

for (const row of data.entries()) {
  const search = row[0].replace('internal_', '');

  if (search.length > 0 && search.substr(1, 3) !== 'ext') {
    currentCTX.data[search][2] = row[1];
  }
  const currentData = JSON.stringify(currentCTX.data);
  const { feedId } = window;
  const params = { userSave: currentData, id: parseInt(feedId, 10), channel: 'import' };
  const res = fetchData(`${apiUrl}mapping`, params).then(() => window.location = '/');
}

However entries() doesn't have forEach() so I can not use it.但是entries()没有 forEach() 所以我不能使用它。

What is a clean way to do the above?执行上述操作的干净方法是什么?

Instead of using for..of , spread the .entries() iterator into an array so you can use forEach on it:不使用for..of ,而是将.entries()迭代器扩展到一个数组中,以便您可以在其上使用forEach

[...data.entries()].forEach((row) => {
  // ...

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

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