I am trying to delete first two, three, four indices of an array. This is my code:
returnFilteredRoles() {
if (this.props.user.role_id == 1 || this.props.user.role_id == 2) {
return this.state.data;
} else if (this.props.user.role_id == 3) {
return this.state.data.splice(0, 2);
} else if (this.props.user.role_id == 4) {
return this.state.data.splice(0, 3);
} else if (this.props.user.role_id == 5) {
return this.state.data.splice(0, 4);
}
}
ex: data = [1,2,3,4,5]
;
if role_id == 3
, it should return [3,4,5]
. instead it returns [1,2]
. no Idea why.
I tried storing the state into a variable and splicing that variable and returning that. But it gives same result.
The thing is, that slice returns the elements that you cut out:
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1, 3);
console.log(citrus); // ["Orange", "Lemon"]
Source: https://www.w3schools.com/jsref/jsref_slice_array.asp