功能仅删除数组中的最后一项

时间:2018-02-06 06:25:58

标签: javascript arrays react-native

在本机反应中,我正在使用这些函数来保存和删除数组中的项目,但它只删除最后一项,我应该更改什么?

SaveValue = () => {
  const newFavs = [...this.state.favs, this.state.UserInput];
  this.setState({ favs: newFavs, UserInput: '' }, () => {
      AsyncStorage.setItem("Favorites", JSON.stringify(this.state.favs));
      Keyboard.dismiss()
  });
};

FetchValue = () => {
  AsyncStorage.getItem("Favorites").then((value) => {
    this.setState({
      favs: JSON.parse(value)
    });
  }).done();
};


RemoveValue(item){
    const index = this.state.favs.indexOf(item);
    const newArray = [...this.state.favs];
    newArray.splice(index,1);
    this.setState({ favs: newArray });
    AsyncStorage.setItem("Favorites", JSON.stringify(newArray));
}

0 个答案:

没有答案