for of 循环无法遍历在 reactjs 中被视为数组的对象的值
for of loop unable to loop through values of an object which has been treated as an array in reactjs
renderRow()
是Reactclass组件中的一个方法,用于获取state operators对象的键值,并将该对象的值传递给react组件arithmeticComponent
将被渲染,但在键上的每个映射上调用 for 循环后无法生成值。问题是对象的值无法作为道具传递到 arithmeticComponent
state = {
operators: {
topOperators: ["C", "Mod", "<"],
numbers: ["%", ".", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
},
};
renderRow() {
{
Object.entries(this.state.operators).map((entry, index) => {
// console.log(entry);
let key = entry[0];
let value = entry[1];
console.log(value);
for (let val of value) {
console.log(value.length);
//console.log(key, value);
return (
<Row className="Row">
{console.log(val)}
<ArithmeticComponent key={index} operator={val} value={val} />;
</Row>
);
}
console.log(key, value);
});
}
state = {
operators: {
topOperators: ["C", "Mod", "<"],
numbers: ["%", ".", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
},
};
renderRow() {
return Object.values(this.state.operators).map((value) => value.map(v=>(
<Row className="Row">
<ArithmeticComponent key={v} operator={v} value={v} />;
</Row>
))
);
}
谨慎使用Array.map中的return语句
并且不应使用 index
作为密钥
renderRow()
是Reactclass组件中的一个方法,用于获取state operators对象的键值,并将该对象的值传递给react组件arithmeticComponent
将被渲染,但在键上的每个映射上调用 for 循环后无法生成值。问题是对象的值无法作为道具传递到 arithmeticComponent
state = {
operators: {
topOperators: ["C", "Mod", "<"],
numbers: ["%", ".", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
},
};
renderRow() {
{
Object.entries(this.state.operators).map((entry, index) => {
// console.log(entry);
let key = entry[0];
let value = entry[1];
console.log(value);
for (let val of value) {
console.log(value.length);
//console.log(key, value);
return (
<Row className="Row">
{console.log(val)}
<ArithmeticComponent key={index} operator={val} value={val} />;
</Row>
);
}
console.log(key, value);
});
}
state = {
operators: {
topOperators: ["C", "Mod", "<"],
numbers: ["%", ".", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
},
};
renderRow() {
return Object.values(this.state.operators).map((value) => value.map(v=>(
<Row className="Row">
<ArithmeticComponent key={v} operator={v} value={v} />;
</Row>
))
);
}
谨慎使用Array.map中的return语句
并且不应使用 index
作为密钥