[英]Remove elements from array using javascript filter
I have two arrays and want to remove duplicates using filter function.我有两个数组,想使用过滤器函数删除重复项。
Here is my code:这是我的代码:
arr1 = [1, 2, 3, 1, 2, 3]; arr2 = [2, 3]; result = [1, 1]; var result = arr1.filter(function(value, index) { for (var i = 0; i <= arr2.length; i++) { if (value !== arr2[i]) { return value === arr2[i]; } } }
Thanks in advance!提前致谢! Any help would be great!任何帮助都会很棒!
You can try to convert arguments into array and then check if the value from the initial array is in arguments array:您可以尝试将参数转换为数组,然后检查初始数组中的值是否在参数数组中:
function destroyer(arr) {
// Converting arguments into array
var args = Array.prototype.slice.call(arguments);
arr = arr.filter(function (val) {
return args.includes(val)===false;
});
return arr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3); // returns[1,1]
First of all, if its not a problem adding a library.首先,如果添加库没有问题。 I am using uniq from underscore.js .我正在使用underscore.js 中的uniq 。
uniq_.uniq(array, [isSorted], [iteratee]) Alias: unique uniq_.uniq(array, [isSorted], [iteratee]) 别名:唯一
Produces a duplicate-free version of the array, using === to test object equality.生成数组的无重复版本,使用 === 测试对象相等性。 In particular only the first occurence of each value is kept.特别是只保留每个值的第一次出现。 If you know in advance that the array is sorted, passing true for isSorted will run a much faster algorithm.如果您事先知道数组已排序,则为 isSorted 传递 true 将运行更快的算法。 If you want to compute unique items based on a transformation, pass an iteratee function.如果要根据转换计算唯一项,请传递 iteratee 函数。
_.uniq([1, 2, 1, 4, 1, 3]); _.uniq([1, 2, 1, 4, 1, 3]); => [1, 2, 4, 3] => [1, 2, 4, 3]
Other solution is using pure JS:其他解决方案是使用纯 JS:
var newArray = [1, 2, 2, 3, 3, 4, 5, 6];
var unique = newArray.filter(function(itm, i, a) {
return i == newArray.indexOf(itm);
});
alert(unique);
But first you will need to combine your arrays in a new array:但首先您需要将您的数组组合到一个新数组中:
var newArray = arr1.concat(arr2);
I hope this helped!我希望这有帮助! :) :)
Here's one way without the filter function:这是没有过滤器功能的一种方法:
var arr1 = [1, 2, 3, 1, 2, 3];
var newArr = [];
for(var i = 0;i < arr1.length;i++){
if (newArr.indexOf(arr1[i]) === -1) {
newArr.push(arr1[i]);
}
}
As in this JS Fiddle , using filter()
就像在这个JS Fiddle 中一样,使用filter()
arr1 = [1, 2, 3, 1, 2, 3]; arr2 = [2, 3]; result = [1, 1]; var result = arr1.filter(myFunc); function myFunc(value) { for (var i = 0; i < arr2.length; ++i) { // to remove every occurrence of the matched value for (var j = arr1.length; j--;) { if (arr1[j] === arr2[i]) { // remove the element arr1.splice(j, 1); } } } } document.getElementById('result').innerHTML = arr1; console.log(arr1); // Output: [1,1]
<div id="result"></div>
Just use Array.prototype.filter()
只需使用Array.prototype.filter()
The
filter()
method creates a new array with all elements that pass the test implemented by the provided function.filter()
方法创建一个新数组,其中包含通过所提供函数实现的测试的所有元素。
with Array.prototype.indexOf()
使用Array.prototype.indexOf()
The
indexOf()
method returns the first index at which a given element can be found in the array, or -1 if it is not present.indexOf()
方法返回可以在数组中找到给定元素的第一个索引,如果不存在,则返回 -1。
var arr1 = [1, 2, 3, 1, 2, 3], arr2 = [2, 3], result = arr1.filter(function (a) { return !~arr2.indexOf(a); }); document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.