【发布时间】:2017-12-10 22:30:16
【问题描述】:
数组的生成方式如下:
for(var i = start; i <= end;i+=step) {
arr[i] = i + 'is the number';
}
数组结构是这样的
arr = [
1100: "1100 is the number",
1101: "1101 is the number",
1102: "1102 is the number",
1103: "1103 is the number",
1104: "1104 is the number",
1105: "1105 is the number",
];
等等。
这个数组将被映射到 React 中的一个列表元素中。
render() {
const list = this.arr;
const timeList = list.map((element) =>
<li>{element}</li>
);
return (
<div className="App" >
{timeList}
</div>
);
它适用于常规数组,但使用这个会给我一个无限循环。
为什么会发生这种情况以及解决这个问题的可能性是什么?
谢谢
更新: 发现 “如果你使用命名索引,JavaScript 会将数组重新定义为标准对象。 之后,一些数组方法和属性会产生不正确的结果。”
但还是没有解决问题
【问题讨论】:
-
您的数组结构不是有效的 JavaScript 代码。 “如果你使用命名索引,JavaScript 会将数组重新定义为标准对象。” 这是错误的。每个数组都是一个对象。但是,并非数组的所有属性都被视为数组的元素。因此,很可能您以不应该使用数组的方式使用数组。但是,您拥有的代码不会导致无限循环。如果没有完整的示例,就很难提供帮助。
标签: javascript arrays reactjs ecmascript-6 mapping