Javascript 为什么这个For循环不改变我的对象数组?JS

Javascript 为什么这个For循环不改变我的对象数组?JS,javascript,jsx,Javascript,Jsx,简单的问题,我确信我是愚蠢的,这是一个很容易解决的问题。拥有一个对象数组,并希望访问每个对象的vis值,并通过我的for循环对其进行更改。我不明白为什么它不起作用 onSearch = keyWord => { let newMarkers = this.state.markers; for (let i = 0; i<this.state.markers.length;i++) { if (this.state.markers[i].name.toLow

简单的问题,我确信我是愚蠢的,这是一个很容易解决的问题。拥有一个对象数组,并希望访问每个对象的vis值,并通过我的for循环对其进行更改。我不明白为什么它不起作用

onSearch = keyWord => {
    let newMarkers = this.state.markers;
    for (let i = 0; i<this.state.markers.length;i++) {
      if (this.state.markers[i].name.toLowerCase().includes(keyWord.toLowerCase())) {
        newMarkers[i].vis = true;
      } else {
        newMarkers[i].vis = false;
      }
    }
    console.log(newMarkers);
    //console.log to see if the vis value has been changed: spoiler alert it hasn't :(
    this.setState({markers: newMarkers});

    console.log(this.state.markers)
  }
onSearch=关键字=>{
让newMarkers=this.state.markers;

for(设i=0;i
setState
是异步的。因此,您需要按如下方式编写代码:

onSearch = keyWord => {
  let newMarkers = this.state.markers;
  for (let i = 0; i<this.state.markers.length;i++) {
    if (this.state.markers[i].name.toLowerCase().includes(keyWord.toLowerCase())) {
      newMarkers[i].vis = true;
    } else {
      newMarkers[i].vis = false;
    }
  }
  console.log(newMarkers);
  this.setState({markers: newMarkers}, () => {
    // use this callback function to do stuff AFTER state changes
    console.log(this.state.markers);
  });
}
onSearch=关键字=>{
让newMarkers=this.state.markers;
for(设i=0;i{
//使用此回调函数在状态更改后执行操作
console.log(this.state.markers);
});
}

setState
是异步的。因此,您需要按如下方式编写代码:

onSearch = keyWord => {
  let newMarkers = this.state.markers;
  for (let i = 0; i<this.state.markers.length;i++) {
    if (this.state.markers[i].name.toLowerCase().includes(keyWord.toLowerCase())) {
      newMarkers[i].vis = true;
    } else {
      newMarkers[i].vis = false;
    }
  }
  console.log(newMarkers);
  this.setState({markers: newMarkers}, () => {
    // use this callback function to do stuff AFTER state changes
    console.log(this.state.markers);
  });
}
onSearch=关键字=>{
让newMarkers=this.state.markers;
for(设i=0;i{
//使用此回调函数在状态更改后执行操作
console.log(this.state.markers);
});
}

在您的情况下,它保持状态不变。为了克服此问题,您可能会对此进行快速修复。请按照代码进行操作

const jsonNewMarkers = JSON.stringify(newMarkers);
this.setState({markers: JSON.parse(jsonNewMarkers)}, () => {
  // use this callback function to do stuff AFTER state changes
  console.log(this.state.markers);
});

请尝试解决方案并让我知道。它应该像我的一样工作。

它在您的情况下保持状态不变。要克服此问题,您可能会对此进行快速修复。请按照代码进行操作

const jsonNewMarkers = JSON.stringify(newMarkers);
this.setState({markers: JSON.parse(jsonNewMarkers)}, () => {
  // use this callback function to do stuff AFTER state changes
  console.log(this.state.markers);
});

请尝试解决方案并让我知道。它应该像我的一样工作。

Console.log永远不会向您显示更改,因为setState是异步操作。但是我的newMarkers也没有更改,它也没有使用setState?我对它做了太多的修改,它现在可以工作了。感谢您的帮助!很高兴听到您发现了我的错误Assue.Happy Learning!Console.log将永远不会向您显示更改,因为setState是异步操作。但是我的newMarkers也没有更改,并且它不使用setState?我对它做了太多的修改,导致输入错误,它现在可以工作了。感谢您的帮助!很高兴听到您发现了这个问题。Happy Learning!我知道了,我将在以后记住这一点ure,谢谢!@ZKJ这是一个更好的答案,因为它使用了setState的预期API,而不是stein馈赠/解析数据的黑客方式来制作副本。我明白了,我以后会记住这一点,谢谢!@ZKJ这是一个更好的答案,因为它使用了setState的预期API,而不是stein馈赠/p的黑客方式用数据来复制。