0

I want to remove the same values from two tables such as:

var array1 = ["1", "2", "3", "4", "5"]
var array2 = ["3", "4", "5"]

And I want to result :

var result = ["1", "2"]



André
  • 4,417
  • 4
  • 29
  • 56
Antonin_04
  • 37
  • 5
  • 4
    please share code what you have tried so far – Naga Sai A Feb 13 '19 at 19:45
  • 2
    @Antonin_04 what if `array2 = ["3", "4", "5","6"]` – Maheer Ali Feb 13 '19 at 19:47
  • 1
    Show your best attempt so far and explain what went wrong with it (errors, unexpected results, etc.). Then we can help you. – p.s.w.g Feb 13 '19 at 19:48
  • @NagaSaiA I've already tried but i don't have found – Antonin_04 Feb 13 '19 at 19:49
  • 2
    @Antonin_04 what if array1 = ["1", "2", "3", "4", "5", "5"]? – Gaurav Saraswat Feb 13 '19 at 19:53
  • 1
    @Antonin_04 This is a Q&A site not a code-generation service. Ideally, we want to help you improve readers understanding of the problem, not just provide a copy/paste solution. Even if your attempt didn't work or didn't even compile, please show your work, so we can point out *where* you went wrong for your benefit as well as for future readers that may be experiencing similar issues. Also, as other commenters have pointed out, the requirements are rather vague on specific edge cases. – p.s.w.g Feb 13 '19 at 19:55

4 Answers4

0

array1 .concat(array2) .filter((v, i, a) => a.indexOf(v) === i)

Stefano Vuerich
  • 996
  • 1
  • 7
  • 28
0

You can use filter and include

    var array1 = ["1", "2", "3", "4", "5"]
    var array2 = ["3", "4", "5"]

    array1.filter((a) => { return !array2.includes(a)});
Saurabh Yadav
  • 3,303
  • 1
  • 10
  • 20
0

Use filter and invert the return from includes:

var array1 = ["1", "2", "3", "4", "5"]
var array2 = ["3", "4", "5"]
var result = array1.filter(e => !array2.includes(e));
console.log(result);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

You can use filter() and includes() and concat(). It will work for both arrays

var array1 = ["1", "2", "3", "4", "5"]
var array2 = ["3", "4", "5","6"]

let result = array1.filter(x => !array2.includes(x)).concat(array2.filter(x => !array1.includes(x)))
console.log(result);
             
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73