2

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!

1
  • It would be great if can be done using the filter function, if not any answer is fine as long the output is the same. Commented Nov 15, 2015 at 21:40

5 Answers 5

2

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]
Sign up to request clarification or add additional context in comments.

Comments

0

First of all, if its not a problem adding a library. I am using uniq from underscore.js.

uniq_.uniq(array, [isSorted], [iteratee]) Alias: unique

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. If you want to compute unique items based on a transformation, pass an iteratee function.

_.uniq([1, 2, 1, 4, 1, 3]); => [1, 2, 4, 3]

Other solution is using pure 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);

JS Fiddle

I hope this helped! :)

2 Comments

Thanks for the answer, but i'm looking for just plain javascript.
As I mentioned the output should be [1,1] the duplicates 2,3 should be removed. Anyway thanks for your effort.
0

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

Comments

0

Just use Array.prototype.filter()

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

with 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.

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

2 Comments

Thanks a lot this is great, but having problems understanding the return statement.
the ! is a boolean not and the ~ is a bitwise not. so if the element is not in the array, then return true.
0

As in this JS Fiddle, using 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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.