【发布时间】:2017-11-02 09:58:13
【问题描述】:
componentWillMount(){
this.expand();
}
componentDidMount(){}
expand(){
var re = [];
this.state.reservation1.length = 9;
for (var i = 0; i < this.state.reservation1.length; i++) {
if(this.state.reservation1[i]){
re[i]=this.state.reservation1[i]
}
else {
re[i]= this.state.reservation1[i]=
{name:"",active:false,dayindex:0}
}
}
console.log(re)
console.log(re.dayindex)
}
constructor(props){
super(props);
this.state = {
reservation1:[
{name:"8:30",active:true,dayindex:1},
{name:"jad",active:true,dayindex:3},
{name:"tony",active:true,dayindex:4},
{name:"",active:false,dayindex:6},
],
reservations:[]
};
this.expand=this.expand.bind(this);
}
返回一个包含 9 个对象的数组 re.dayindex 未定义 有人可以帮忙吗
我正在尝试扩展此数组以按 dayindex 对其重新排序 任何解决方案都会很高兴
【问题讨论】:
-
还有 reservation1.dayindex 未定义
-
re是一个数组,因此您也需要将其作为数组访问。re[0].dayindex -
您应该使用setState 在构造函数之外更新状态。你应该not mutate。
标签: javascript algorithm reactjs loops