简体   繁体   English

JavaScript中array.splice的替代

[英]Alternative to array.splice in JavaScript

I am currently working on a project where I store numeric values in a JS array.我目前正在从事一个将数值存储在 JS 数组中的项目。 After some changes it should be removed again.经过一些更改后,它应该再次被删除。 I currently use the array.splice method like this:我目前使用这样的 array.splice 方法:

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

But this seems to give me issues on Safari. This piece of code works in every browser, like Chrome, Firefox, Opera.但这似乎给我带来了关于 Safari 的问题。这段代码适用于所有浏览器,例如 Chrome、Firefox、Opera。 But not on Safari. It even works in the Technical Preview of Safari.但不适用于 Safari。它甚至适用于 Safari 的技术预览版。

Does anyone have an alternative?有人有其他选择吗?

Thanks in advance:)提前致谢:)

You have to slice before and after the index, and concat the results.您必须在索引前后切片,并concat结果。 Note that Array.prototype.slice() doesn't mutate the original array like Array.prototype.splice() does.请注意, 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);

Or using ES6 and array spread:或者使用 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);

You can use the built-in filter()您可以使用内置的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);

Another Alternative to array.splice in JavaScript is array.reduce另一种替代array.splice在JavaScript是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);

试试slice()方法

arr = arr.slice(index, 1 );

Sorry for late but hopefully it is useful for someone else抱歉迟到了,但希望它对其他人有用

 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; }

Or you can use simple version (NOTE: it is simple but reversed)或者你可以使用简单的版本(注意:它很简单但相反)

 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)); }); }

Or if you have to remove just one element then use this或者,如果您只需要删除一个元素,请使用它

 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; }); }

Some more ideas:更多的想法:

Option A flatMap() :选项 A flatMap()

Return an empty [] in order to "filter" elements.返回一个空[]以“过滤”元素。 Less efficient but might be useful in case you want to add new elements as well.效率较低,但如果您还想添加新元素,可能会有用。

 const a = [3, 4, 5, 6]; const filter = 2; const r = a.flatMap((v, j) => j?== filter: v; []). console:log(`Result, %o`; r): // Result, [3, 4, 6]

Example for filter + insert过滤器 + 插入的示例

 const a = [3, 4, 5, 6]; const filter = 2; const insert = 1; const value = 4.5; const r = a.flatMap((v, j) => { if (j === filter) return []; if (j === insert) return [v, value]; return v; }); console.log(`Result: %o`, r); // Result: [3, 4, 4.5, 6]


Option B Array.from() :选项 B Array.from()

 const a = [3, 4, 5, 6]; const filter = 2; const r = Array.from({length: a.length -1}, (_, i) => a[i >= filter? i + 1: i]); console.log(`Result: %o`, r); // Result: [3, 4, 6]


Option C "Destructure":选项 C“解构”:

 const a = [3, 4, 5, 6]; const filter = 2; const {[filter]: _, ...o} = a; const r = Object.values(o); console.log(`Result: %o`, r); // Result: [3, 4, 6]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM