Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created August 13, 2019 23:13
Show Gist options
  • Save parzibyte/9ccbd96b01c88daaaf6ca42e37477a63 to your computer and use it in GitHub Desktop.
Save parzibyte/9ccbd96b01c88daaaf6ca42e37477a63 to your computer and use it in GitHub Desktop.
/**
* Diferencia de arreglos en JavaScript
* Obtener elementos que existen en arreglo A pero no en arreglo B
*
* @author parzibyte
*
* https://parzibyte.me/blog
*
*/
// Obtener elementos que existen en un arreglo
// pero no en otro
const diferenciaDeArreglos = (arr1, arr2) => {
return arr1.filter(elemento => arr2.indexOf(elemento) == -1);
}
const marioWorld = [
"Mario",
"Luigi",//diferencia
"Yoshi",// diferencia
"Bowser"
];
const marioBros3 = [
"Mario",
"Bowser"
];
const existenEnWorldPeroNoEn3 = diferenciaDeArreglos(marioWorld, marioBros3);
console.log("Existen en World pero no en 3: ", existenEnWorldPeroNoEn3);
const existenEn3PeroNoEnWorld = diferenciaDeArreglos(marioBros3, marioWorld);
console.log("Existen en 3 pero no en World: ", existenEn3PeroNoEnWorld);
/*
Salida
Existen en World pero no en 3: [ 'Luigi', 'Yoshi' ]
Existen en 3 pero no en World: []
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment