【问题标题】:How to remove the next element after the first?如何在第一个元素之后删除下一个元素?
【发布时间】:2019-11-17 22:55:06
【问题描述】:

我们的目标是获取与我所拥有的条件匹配的数组的所有其他元素,但我无法做到。

所以,我开始尝试在Array.prototype.indexOf()MDN 中找到的另一个示例。

var beasts = ['ant', 'bison', 'camel', 'duck', 'bison', 'duck', 'duck', 'bison', "duck", 'bison',"camel", "duck", "duck"];

beasts.splice(beasts.indexOf("bison",beasts.indexOf("bison"+1)),1);
console.log(beasts);

我希望它会从数组中删除第二个“野牛”,但它会删除最后一个元素“鸭子”...

我能在这里做什么? (当然我可能还没有学会这些东西的正确语法)

【问题讨论】:

  • 您当前正在搜索bison1。它应该是:beasts.indexOf("bison") + 1 而不是 beasts.indexOf("bison"+1)
  • var aaa = [1,2,3,4,5]; aaa.splice(1,1); aaa;

标签: javascript indexof splice


【解决方案1】:

这是工作方式

let beasts = ['ant', 'bison', 'camel', 'duck', 'bison', 'duck', 'duck', 'bison', "duck", 'bison',"camel", "duck", "duck"];

beasts.splice(beasts.indexOf("bison",beasts.indexOf("bison")),1);
console.log(beasts);

【讨论】:

    【解决方案2】:

    你需要修正你的 indexOf 语法

    beasts.indexOf("bison"+1)  // this searches for `bison1`
    

    到这里

    beasts.indexOf("bison") + 1
    

    var beasts = ['ant', 'bison', 'camel', 'duck', 'bison', 'duck', 'duck', 'bison', "duck", 'bison',"camel", "duck", "duck"];
    
    beasts.splice(beasts.indexOf("bison",beasts.indexOf("bison")+1),1)
    console.log(beasts);

    【讨论】:

      【解决方案3】:

      您的拼接代码只有一个错误,已在 cmets 中发现(@adiga 所说的It should be: beasts.indexOf("bison") + 1 and not beasts.indexOf("bison"+1))。

      不过,我的猜测是,您实际上想从列表中删除'bison'所有 个实例。我可能错了,但这是一个猜测。您可以使用过滤器来做到这一点:

      var beasts = ['ant', 'bison', 'camel', 'duck', 'bison', 'duck', 'duck', 'bison', "duck", 'bison',"camel", "duck", "duck"];
      
      const updatedBeasts = beasts.filter(beast => beast !== 'bison');
      
      console.dir(updatedBeasts);

      【讨论】:

      • 是的,@adiga 说的是我需要的修复!但我想要达到的最终结果是从列表中删除所有其他野牛。
      • @KarolisVaitkevicius 我上面的代码将删除所有个野牛,你只需要删除1个吗?
      • 我需要在第一个之后删除所有其他的。所以,删除 bison1、bison3、bison5 等...@OliverRadini
      【解决方案4】:

      问题是你要去bison+1而不是bison所以试试这个:

      var beasts = ['ant', 'bison', 'camel', 'duck', 'bison', 'duck', 'duck', 'bison', "duck", 'bison',"camel", "duck", "duck"];
      
      beasts.splice(beasts.indexOf("bison",beasts.indexOf("bison")),1);
      console.log(beasts);

      【讨论】:

        【解决方案5】:

        您的代码中有一些小错误会导致您的输出。
        首先,要从数组中删除一个元素,我们可以使用splice() 方法,如下所示。

        array.splice(starting_index_from_which_the elements_are_to_be_deleted, number_of_elements_to_be_deleted)

        如果大于数组的长度,则将开始设置为数组的长度。
        您的代码的正确格式是:

        var beasts = ['ant', 'bison', 'camel', 'duck', 'bison', 'duck', 'duck', 'bison', "duck", 'bison',"camel", "duck", "duck"];
        
        beasts.splice(beasts.indexOf("bison"),beasts.indexOf("bison")+1);
        console.log(beasts);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-12-20
          • 1970-01-01
          • 1970-01-01
          • 2015-04-20
          • 1970-01-01
          • 1970-01-01
          • 2011-01-15
          相关资源
          最近更新 更多