-1

i have two arrays that both contains HTML Elements, i want to remove from the first array all the elements that are also present in the second one.

var a = [document.createElement('div'), document.createElement('span')],
    b = [document.createElement('span'), document.createElement('p')];

filter(a,b) == [document.createElement('span')] // true

i tried with something like this, but it seems like it doesn't work:

var filter = function(a,b) {
   return a.filter(element => {
      return b.map(element_ => {
         return element.isEqualNode(element_);
      }).includes(false);
   });
}
Giuppox
  • 1,393
  • 9
  • 35

1 Answers1

0
let res = a.filter((el)=> { return b.find((el2)=> el.localName === el2.localName)});

// res = [span];

So, el.localName is a property of a HTML element of type string which contains the name of the element. Which can be used for comparing two HTML elements

For Example: In Case of div its 'div'

Code returns the common element present in both the arrays.

Ravi Rajput
  • 539
  • 4
  • 12
  • Hello and welcome to SO! While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. Please read the [tour](https://stackoverflow.com/tour), and [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – Mhd Alaa Alhaj Jan 20 '21 at 09:22
  • 1
    can you be more accurate explaining better how your code works? – Giuppox Jan 20 '21 at 11:24
  • @Giuppox let me know if it worked for you? – Ravi Rajput Jan 21 '21 at 07:04
  • yeah! it works, thanks :). You know that in arrow functions, if there is only one parameter, you can remove the parenthesis? – Giuppox Jan 21 '21 at 07:51
  • Yes, Can I expect a upvote for my ans? – Ravi Rajput Jan 21 '21 at 09:55