Reputation: 505
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);
}
RE RETURN AN ARRAY OF 9 OBJECTS re.dayindex is undefined can someone help please
i am trying to expand this array to reorder it by dayindex any solution will be pleased
Upvotes: 0
Views: 1632
Reputation: 8114
You are trying to access an object under in an array. So it should be re[i].dayindex
, i being your index.
P.S: You should try not to mutate things in javascript, it really makes your code vulnerable. re[i]= this.state.reservation1[i]
?
Maybe you can try this code:-
const re = [];
const 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},
];
reservation1.map((datum,index)=> {
if (datum) {
re.push(datum)
} else {
re.push({
name: "",
active: false,
dayIndex: 0
});
}
})
console.log(re[1].dayindex);
Upvotes: 1