如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
当前回答
2017-05-08
大多数给定的回答都用于严格的比较, 意思是两个对象在内存( 或原始类型) 中引用完全相同的对象, 但通常您想要从具有一定值的数组中删除一个非原始对象。 例如, 如果您给服务器打电话, 并想要对照本地对象检查已检索到的对象 。
const a = {'field': 2} // Non-primitive object
const b = {'field': 2} // Non-primitive object with same value
const c = a // Non-primitive object that reference the same object as "a"
assert(a !== b) // Don't reference the same item, but have same value
assert(a === c) // Do reference the same item, and have same value (naturally)
//Note: there are many alternative implementations for valuesAreEqual
function valuesAreEqual (x, y) {
return JSON.stringify(x) === JSON.stringify(y)
}
//filter will delete false values
//Thus, we want to return "false" if the item
// we want to delete is equal to the item in the array
function removeFromArray(arr, toDelete){
return arr.filter(target => {return !valuesAreEqual(toDelete, target)})
}
const exampleArray = [a, b, b, c, a, {'field': 2}, {'field': 90}];
const resultArray = removeFromArray(exampleArray, a);
//resultArray = [{'field':90}]
数值AreEqual有替代/更快的操作,但这样可以操作。如果您有特定的字段要检查,也可以使用自定义的比较器(例如,有些已检索的 UUID 相对于本地的 UUID ) 。
2. 还注意到这是一个功能操作,意即它不改变原始阵列。
其他回答
定义:
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);
从数组中删除一个特定元素/字符串可在单班条中进行:
theArray.splice(theArray.indexOf("stringToRemoveFromArray"), 1);
此处:
阵列:要从
将字符串从矩阵中移除:要删除的字符串,而1是要删除的元素数量。
注 注 注 注 注注:如果“字符串要从阵列中移除”不位于数组中,这将删除数组的最后一个元素。
在移除该元素之前先检查该元素是否存在于您的阵列中, 总是很好的做法 。
if (theArray.indexOf("stringToRemoveFromArray") >= 0){
theArray.splice(theArray.indexOf("stringToRemoveFromArray"), 1);
}
取决于客户电脑上是否有新版或旧版的剪贴条:
var array=['1','2','3','4','5','6']
var newArray = array.filter((value)=>value!='3');
或
var array = ['1','2','3','4','5','6'];
var newArray = array.filter(function(item){ return item !== '3' });
“ 3” 是您想要从数组中删除的值。 然后, 数组将变成 :['1','2','4','5','6']
更新 :只有当您无法使用 ECMASCript 2015 (前称ES6) 时,才会推荐使用这种方法。 如果您可以使用这种方法, 其它答案则提供更清晰的落实。
这个格子在这里将解决您的问题, 并删除所有出现参数的情况, 而不是仅一个( 或指定值) 。
Array.prototype.destroy = function(obj){
// Return null if no objects were found and removed
var destroyed = null;
for(var i = 0; i < this.length; i++){
// Use while-loop to find adjacent equal objects
while(this[i] === obj){
// Remove this[i] and store it within destroyed
destroyed = this.splice(i, 1)[0];
}
}
return destroyed;
}
用法 :
var x = [1, 2, 3, 3, true, false, undefined, false];
x.destroy(3); // => 3
x.destroy(false); // => false
x; // => [1, 2, true, undefined]
x.destroy(true); // => true
x.destroy(undefined); // => undefined
x; // => [1, 2]
x.destroy(3); // => null
x; // => [1, 2]
有许多方法可以删除指定元素从 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项)更适合解决上述问题。