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 :: set node_env in windows 
Javascript :: change select value jquery 
Javascript :: scroll to top 
Javascript :: angular jspdf 
Javascript :: javascript onclick button 
Javascript :: find by array of ids mongoose 
Javascript :: node express app.listen at specific port & host 
Javascript :: path resolve in node js to current dir 
Javascript :: jquery get dropdown list selected value 
Javascript :: date masking javascript to not allow / 
Javascript :: mongodb push to index 
Javascript :: vue 3 script setup dynamic component sample 
Javascript :: ajax select2 
Javascript :: sequelize change column 
Javascript :: discord.js v13 if dm 
Javascript :: wait js 
Javascript :: javascript ready state 
Javascript :: firebase.database.ServerValue.TIMESTAMP 
Javascript :: regular expression characters 
Javascript :: how to return a factorial in javascript 
Javascript :: node js on macbook m1 
Javascript :: babylon js camera position 
Javascript :: select selected option value jquery 
Javascript :: print element by xpath javascript 
Javascript :: how to check password and confirm passwor in joi 
Javascript :: npm run build npm ERR! Missing script: "build" for firebase 
Javascript :: discordjs v13 get message content 
Javascript :: jquery get all inputs in form 
Javascript :: delete message discord.js 
Javascript :: javascript check if object is null or empty 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =