【问题标题】:Alternative to array.splice in JavaScriptJavaScript 中 array.splice 的替代方案
【发布时间】:2017-04-05 01:13:18
【问题描述】:

我目前正在做一个项目,我将数值存储在 JS 数组中。经过一些更改后,它应该再次被删除。我目前使用这样的 array.splice 方法:

function removeA(arr, element) {
    var index = arr.indexOf(element);
    if (index >= 0) {
        arr.splice(index, 1 );
    }
    return arr;
} 

但这似乎给我在 Safari 上的问题。这段代码适用于所有浏览器,例如 Chrome、Firefox、Opera。但不是在 Safari 上。它甚至可以在 Safari 的技术预览版中使用。

有人有替代品吗?

提前致谢:)

【问题讨论】:

  • 有什么问题?
  • 它没有正确删除元素
  • 它做了什么?
  • 这是基本的 JS,所以如果这不起作用,你的 index 有问题。
  • @OriDrori 但这不会让它无处可去吗?

标签: javascript arrays safari splice


【解决方案1】:

试试slice()方法

arr = arr.slice(index, 1 );

【讨论】:

  • 如果要删除指定索引处的元素,将所有其他元素保留在数组中,slice 不会给您相反的结果,只留下索引处指定的元素的数组,“删除”其他所有内容?
【解决方案2】:

你可以使用内置的filter()

var array = [1,2,3,7,4,5,6,7,12,54,7,691];
var array = array.filter(x => x !== 7);
console.log(array);

【讨论】:

  • 我的回复好像没有保存。您的解决方案也适用于除 safari 之外的所有浏览器。该项目没有被正确删除。
【解决方案3】:

你必须在索引前后切片,concat 结果。请注意,Array.prototype.slice() 不会像 Array.prototype.splice() 那样改变原始数组。

var arr = [0, 1, 2, 3, 4, 5, 6, 7];
var index = 5;

var result = arr.slice(0, index).concat(arr.slice(index + 1));

console.log(result);

或者使用 ES6 和数组传播:

var arr = [0, 1, 2, 3, 4, 5, 6, 7];
var index = 5;

var result = [...arr.slice(0, index), ...arr.slice(index + 1)];

console.log(result);

【讨论】:

    【解决方案4】:

    JavaScript 中 array.splice 的另一种替代方法是 array.reduce

    var arr =[1,2,3,2,4,5,6,2];
    var newarr = arr.reduce((acc, elem) => elem !== 2 ? acc.concat(elem) : acc, []);
    console.log(newarr);

    【讨论】:

      【解决方案5】:

      抱歉迟到了,但希望它对其他人有用

      var arr = [32, 33, 16, 40, 55, 2, 41, 3, 10];
      
      document.write("Array : "+arr);
      document.write("<br>");
      document.write("Removed Elements : "+mySplice(arr,2,2));
      document.write("<br>");
      document.write("Processed Array : "+arr);
      
      
      function mySplice(array,index,count) {
        var fixIndex = -1;
        var ret = [];
        arr = array.filter(function(element) {
          fixIndex++;
          if((fixIndex >= index && fixIndex < (index+count))) {
              ret[ret.length]=element;
              return false;
          } else {
              return true;
          }
        });
        return ret;
      }

      或者你可以使用简单的版本(注意:它很简单但相反)

      var arr = [32, 33, 16, 40, 55, 2, 41, 3, 10];
      document.write("Array : "+arr);
      document.write("<br>");
      document.write("Processed Array : "+mySplice_simple(arr,2,2));
      
      
      function mySplice_simple(arr,index,count) {
        fixIndex = -1;
        return arr.filter(function(i) {
          fixIndex++;
          return !(fixIndex >= index && fixIndex < (index+count));
        });
      }

      或者如果你只需要删除一个元素,那么使用这个

      var arr = [32, 33, 16, 40, 55, 2, 41, 3, 10];
      document.write("Array : "+arr);
      document.write("<br>");
      document.write("Processed Array : "+mySplice_simple_v2(arr,2));
      
      
      function mySplice_simple_v2(arr,index,count) {
        fixIndex = -1;
        return arr.filter(function(i) {
          fixIndex++;
          return fixIndex != index;
        });
      }

      【讨论】:

        猜你喜欢
        • 2014-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-19
        相关资源
        最近更新 更多