Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Difference between elements of two arrays js

////Difference b/w elements of two arrays
const a = [1, 2, 3, 4, 5];
const b = [1, 2];

var difference = a.filter((x) => !b.includes(x));
console.log(difference);
//[3,4,5]
Comment

compare two arrays and return the difference javascript

let difference = arr1
                 .filter(x => !arr2.includes(x))
                 .concat(arr2.filter(x => !arr1.includes(x)));
Comment

return symmetric difference of the array javascript

function symmetricDiff(arrary1, array2){
 let symmetricDifference = []; 
  
 symmetricDifference = array1.filter(element => !array2.includes(element))
   .concat(array2.filter(element => !array1.includes(element)))
 return symmetricDifference;
}
Comment

How To Get The Difference Between Two Arrays In JavaScript

function arr_diff (a1, a2) {

    var a = [], diff = [];

    for (var i = 0; i < a1.length; i++) {
        a[a1[i]] = true;
    }

    for (var i = 0; i < a2.length; i++) {
        if (a[a2[i]]) {
            delete a[a2[i]];
        } else {
            a[a2[i]] = true;
        }
    }

    for (var k in a) {
        diff.push(k);
    }

    return diff;
}

console.log(arr_diff(['a', 'b'], ['a', 'b', 'c', 'd']));
console.log(arr_diff("abcd", "abcde"));
console.log(arr_diff("zxc", "zxc"));
 Run code snippet
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery validation stop form submit 
Javascript :: how to use post method axios 
Javascript :: typeof date 
Javascript :: how to edit message discord.js 
Javascript :: angular subscribe on value change 
Javascript :: how to generate random text in vue js 
Javascript :: how to repeat string in javascript 
Javascript :: javascript string error 
Javascript :: navbar route with params vue 
Javascript :: how to concatenate a string in javascript 
Javascript :: regex char or char 
Javascript :: javascript get cpu cores 
Javascript :: document.queryselector picks first or last 
Javascript :: vue js get routes 
Javascript :: mongoose encrypt password 
Javascript :: invariant failed: you should not use <link outside a <router 
Javascript :: accessing nested objects in javascript 
Javascript :: array includes javascript 
Javascript :: why app.use(cors()) not workin 
Javascript :: nullish coalescing operator 
Javascript :: node cron npm how to use 
Javascript :: ajax loader 
Javascript :: import npm dotenv package 
Javascript :: chrome.runtime.sendMessage 
Javascript :: enable swipe using javascript 
Javascript :: mouse position 
Javascript :: how to print two arrays together 
Javascript :: javascript loops 
Javascript :: addeventlistener classlist toggle dom 
Javascript :: headers with fetch 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =