如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
当前回答
有许多方法可以删除指定元素从 JavaScript 阵列中得出的。 以下是我研究中可以想到的五种最佳方法。
1. 直接使用“ splice () ” 方法
在以下代码段,从数组中删除特定预定位置的元素。
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log("Original array: " + arr);
var removed = arr.splice(4, 2);
console.log("Modified array: " + arr);
console.log("Elements removed: " + removed);
2. 使用“ splice() () ” 方法用“ valice () ” 方法用“ value () ” 删除元素
在以下代码段的以下代码段中,如果条件在用于循环。
var arr = [1, 2, 6, 3, 2, 6, 7, 8, 9, 10];
console.log("Original array: " + arr);
for (var i = 0; i < arr.length; i++) {
if (arr[i] === 6) {
var removed = arr.splice(i, 1);
i--;
}
}
console.log("Modified array: " + arr); // 6 is removed
console.log("Removed elements: " + removed);
3. 使用“过滤器()”方法删除按值选择的元素
与使用“ spice() () ” 方法的执行类似, 但它没有改变现有的数组, 而是创造了新的元素阵列, 从而删除了不想要的元素 。
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var filtered = array.filter(function(value, index, arr) {
return value != 6 ;
});
console.log("Original array: "+array);
console.log("New array created: "+filtered); // 6 is removed
4. 在“ Lodash” JavaScript 库中使用“ remove () ” 方法
在下面的代码段中, JavaScript 库里有叫作“ Lodash ” 的删除() 方法。 这个方法也与过滤方法相似 。
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log("Original array: " + array);
var removeElement = _.remove(array, function(n) {
return n === 6;
});
console.log("Modified array: " + array);
console.log("Removed elements: " + removeElement); // 6 is removed
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
5. 制作自定义除去方法
JavaScript没有本地的“ array. remove” 方法, 但我们可以创建一种使用以上方法的方法, 用于在以下代码片段中执行 。
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
function arrayRemove(arr, value) {
return arr.filter(function(element) {
return element != value;
});
}
console.log("Original array: " + array);
console.log("Modified array: " + arrayRemove(array, 6)); // 6 is removed
最后方法(第5项)更适合解决上述问题。
其他回答
定义:
function RemoveEmptyItems(arr) {
var result = [];
for (var i = 0; i < arr.length; i++) if (arr[i] != null && arr[i].length > 0) result.push(arr[i]);
return result;
}
用法 :
var arr = [1,2,3, "", null, 444];
arr = RemoveEmptyItems(arr);
console.log(arr);
要从数组中删除元素, 要从数组中除去元素, 组合、 过滤和删除
每个阵列都有自己的索引,它有助于用索引删除一个特定元素。
拼盘 () 方法
array.splice(index, 1);
第一个参数是指数指数指数指数第二个是要从该索引中删除的元素数量。
因此,单一个元素,我们用1。
删除方法
delete array[index]
过滤 () 方法
如果您想要删除一个在数组中重复的元素, 那么过滤数组 :
removeAll = array.filter(e => e != elem);
何处处elem
是要从数组中删除的元素。array
您的数组名称。
从数组中删除项目的最佳方法是使用过滤法。.filter()
返回没有过滤过的项目的新数组。
items = items.filter(e => e.id !== item.id);
这个.filter()
将当前项目推到过滤的阵列中。更多信息过滤过滤器 在这里.
除了所有这些解决方案之外, 它也可以用阵列来完成. 减量...
const removeItem =
idx =>
arr =>
arr.reduce((acc, a, i) => idx === i ? acc : acc.concat(a), [])
const array = [1, 2, 3]
const index = 1
const newArray = removeItem(index)(array)
console.log(newArray) // logs the following array to the console : [1, 3]
...或者一个循环函数(诚实地说不是那么优雅...也许有人有更好的循环解决方案? ? )...
const removeItemPrep =
acc =>
i =>
idx =>
arr =>
// If the index equals i, just feed in the unchanged accumulator(acc) else...
i === idx ? removeItemPrep(acc)(i + 1)(idx)(arr) :
// If the array length + 1 of the accumulator is smaller than the array length of the original array concatenate the array element at index i else...
acc.length + 1 < arr.length ? removeItemPrep(acc.concat(arr[i]))(i + 1)(idx)(arr) :
// return the accumulator
acc
const removeItem = removeItemPrep([])(0)
const array = [1, 2, 3]
const index = 1
const newArray = removeItem(index)(array)
console.log(newArray) // logs the following array to the console : [1, 3]
如果数组含有重复的值,而您想要删除目标的所有发生点, 那么这就是前进的道路...
let data = [2, 5, 9, 2, 8, 5, 9, 5];
let target = 5;
data = data.filter(da => da !== target);
注: - 过滤器不会改变原始数组;相反它会创建一个新的数组。
所以再次分配很重要。
这又引出了另一个问题,你无法制造变数const
应该是:let
或var
.