简体   繁体   English

为什么我的接头会删除除第一个之外的所有接头,而不是仅删除指定的接头?

[英]Why is my splice deleting all but the first instead of only the one specified?

For a given array, when I run the splice everything but the first entry is deleted.对于给定的数组,当我运行拼接时,除了第一个条目之外的所有内容都被删除。 However I would expect only the selected entry to be deleted.但是,我希望只删除选定的条目。 I suspect this has something to do with how my i is set up but I'm not sure what the problem is.我怀疑这与我的 i 设置方式有关,但我不确定问题是什么。

removethisone = (i) => {this.setState(state=>({list: state.list.splice(i,1)}))}

    <ul>
    {this.state.list.map((entry, i)=><li key={i}><button onClick={this.removethisone}>{i+1}</button>{entry}</li>)}
    </ul>

Your code is working exactly as you described because that is how splice works .您的代码完全按照您的描述工作,因为这就是splice 的工作方式

The splice method returns an array of the items that are removed. splice 方法返回一个包含被删除项目的数组。 You are storing the items you removed back into the state.您正在将移除的项目存储回状态。 Hence why you get what you removed.因此,为什么你会得到你删除的东西。

removethisone = (i) => {
  this.setState(state => {
    const list = state.list;
    const removedElems = list.splice(i, 1);
    console.log(removedElems);
    return {
      list
    }
  })
}

The reason this is happening is because the Array.splice method returns the removed items from the array.发生这种情况的原因是Array.splice方法返回从数组中删除的项目。

You need to do something like this:你需要做这样的事情:

removethisone = (i) => {
  this.setState(state => {
    const nextState = [ ...state.list ]
    nextState.splice(i, 1)

    return { list: nextList }
  })
}

For example, run the code below to see the ouput:例如,运行下面的代码以查看输出:

 // This is what your code does let numbers = [1, 2, 3, 4, 5]; let removedNumbers = numbers.splice(2, 1) numbers = removedNumbers console.log(numbers); console.log(removedNumbers);

为什么我的JS function 只操作一个<div>分配给 class 而不是全部?</div><div id="text_translate"><p> 我有这个 JS function 应该最初隐藏一个具有 class 名称“tw”的,当点击一个按钮时它应该使它可见。 但是,每当我单击按钮时,它只会更改一个 div 的可见性。 我有 4 个。我该如何解决这个问题?</p><pre> function myFunction(){ var elms = document.getElementsByClassName("tw"); Array.from(elms).forEach((x) =&gt; { if (x.style.display === "block") { x.style.display = "none"; } else { x.style.display = "block"; } }) }</pre><p> <a href="https://jsfiddle.net/qm8bxryh/307/" rel="nofollow noreferrer">https://jsfiddle.net/qm8bxryh/307/</a>这是小提琴</p></div> - Why does my JS function only manipulate one <div> that is assigned to the class instead of all?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 只有一个参数的拼接不会删除整个数组 - splice with only one parameter not deleting the whole array 为什么 splice 会删除我的整个数组? 如果 max 在位置 0,我只想删除它 - Why is splice deleting my entire array? I would only like to delete max if it is in position 0 为什么我的 splice 方法会删除选定索引之后的所有对象? (javascript) - Why is my splice method deleting all objects past the selected index? (javascript) 为什么我的jquery删除列表中的第一个元素而不是指定的元素? - Why is my jquery removing the first elements in a list instead of specified ones? 为什么所有 arrays 都删除此集合中的元素而不是被引用的元素? - Why are all of the arrays deleting an element in this set instead of the one being referenced? 我怎样才能让我的 Javascript 代码 select 在页面上显示所有“b”标签,而不是只有第一个? - How can I make my Javascript code select all 'b' tags on a page instead of only the first one? 为什么我的Delete函数只用Express删除Express中的第一个元素 - Why my Delete function deleting only First element in express with jquery 为什么我的JS function 只操作一个<div>分配给 class 而不是全部?</div><div id="text_translate"><p> 我有这个 JS function 应该最初隐藏一个具有 class 名称“tw”的,当点击一个按钮时它应该使它可见。 但是,每当我单击按钮时,它只会更改一个 div 的可见性。 我有 4 个。我该如何解决这个问题?</p><pre> function myFunction(){ var elms = document.getElementsByClassName("tw"); Array.from(elms).forEach((x) =&gt; { if (x.style.display === "block") { x.style.display = "none"; } else { x.style.display = "block"; } }) }</pre><p> <a href="https://jsfiddle.net/qm8bxryh/307/" rel="nofollow noreferrer">https://jsfiddle.net/qm8bxryh/307/</a>这是小提琴</p></div> - Why does my JS function only manipulate one <div> that is assigned to the class instead of all? 为什么拼接方法只删除React中的第一个数组索引? - Why is splice method removing only first array index in React? Git 只添加我的一个文件夹而不是所有文件夹 - Git adding only one of my folders instead of all of them
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM