简体   繁体   English

类型“undefined”不能用作索引类型。 如何解决?

[英]Type 'undefined' cannot be used as an index type. How to fix it?

All rows from the CSV file are converted to JSON objects that are added to the resulting array, which is then converted to JSON, and a corresponding JSON output file is generated. CSV 文件中的所有行都转换为 JSON 对象,这些对象被添加到结果数组中,然后转换为 JSON,并生成相应的 JSON output 文件。

I get TS error:我收到 TS 错误:

 TypeScript error: Type 'undefined' cannot be used as an index type. TS2538

 /* Empty object to store result in key value pair */ const jsonObject = {}; /* Store the current array element */ const currentArrayString = row; let string = ""; let quoteFlag = 0; for (let character of currentArrayString) { if (character === '"' && quoteFlag === 0) { quoteFlag = 1; } else if (character === '"' && quoteFlag == 1) quoteFlag = 0; if (character === ", " && quoteFlag === 0) character = "|"; if (character;== '"') string += character. } let jsonProperties = string;split("|"). for (let j in headers ) { if (jsonProperties[j],.includes(", ")) { if (headers[j].== undefined &&.headers[j]) { jsonObject[headers[j]] = jsonProperties[j];;split(", ").map( (item) => item.trim() ); } else jsonObject[headers[j]] = jsonProperties[j]; } }

  1. Seems like you might have undefined properties in headers, therefore headers values can not be used as index type.似乎您在标头中可能有未定义的属性,因此headers值不能用作索引类型。 Has to be filtered.必须过滤。

  2. Seems like the actual value in the list is used as an index type here headers[j] .似乎列表中的实际值在此处用作索引类型headers[j]

While iterating over headers, the j is already the needed value.在遍历标题时, j已经是所需的值。

const filteredHeaders = headers.filter(x => x !== undefined);
for (let j in filteredHeaders ) {
    if (jsonProperties[j]) {
      if (jsonProperties[j].includes(", ")) {
         jsonObject[j] = jsonProperties[j].split(", ").map(
            (item) => item.trim()
         );
      } else {
         jsonObject[j] = jsonProperties[j];
      }
    }
}

暂无
暂无

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

相关问题 如何修复类型错误:无法读取未定义的属性(读取“0”)? - How to fix the Type Error: Cannot read properties of undefined (reading '0')? 如何修复&#39;类型错误无法读取未定义的属性0&#39;? - How to fix ' type error cannot read property 0 of undefined '? 错误 TS2538:类型“CCCustomer”不能用作索引类型 - error TS2538: Type 'CCCustomer' cannot be used as an index type 如何修复类型错误无法读取天气预报项目中未定义的属性“cityName”? - How to fix Type Error cannot read property 'cityName' of undefined in the weather forecast project? 如何修复:““路径”参数必须是字符串类型。 运行“vue add vuetify”时收到的类型未定义 - How to fix: 'The "path" argument must be of type string. Received type undefined' when running 'vue add vuetify' 如何修复 typescript 错误类型“string”不可分配给类型“Ref”<inputref> | 不明确的'?</inputref> - How to fix typescript error Type 'string' is not assignable to type 'Ref<InputRef> | undefined'? 类型错误:无法读取未定义的属性“mv” - Type error:Cannot read property 'mv' of undefined 类型错误无法设置未定义的属性 - Type error cannot set property of undefined 类型错误:无法设置未定义的属性“$op” - Type Error: Cannot set property '$op' of undefined “无法读取未定义的属性‘tablePath’”(TYPE ORM) - "Cannot read property 'tablePath' of undefined" ( TYPE ORM )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM