[英]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]; } }
Seems like you might have undefined properties in headers, therefore headers
values can not be used as index type.似乎您在标头中可能有未定义的属性,因此
headers
值不能用作索引类型。 Has to be filtered.必须过滤。
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.