Compare two arrays and create two new arrays with missing and new elements using pure javascript or jquery -


how compare 2 javascript arrays , create 2 new arrays of missing , new elements? array elements strings or numbers , not 100% sure sorted in way.

var array1= ['11', '13', '14', '18', '22', '23', '25']; var array2= ['11', '13', '15', '16', '17', '23', '25', '31']; var missing_elements = []; var new_elements = [];  ***required output:*** missing_elements= ['14', '18', '22'] new_elements= ['15', '16', '17', '31'] 

well simple solution iterate on array1, testing elements 1 @ time .includes() produce list of missing elements, reverse list of new elements.

you use .filter() , arrow functions keep short:

var array1= ['11', '13', '14', '18', '22', '23', '25'];  var array2= ['11', '13', '15', '16', '17', '23', '25', '31'];    var missing_elements = array1.filter(v => !array2.includes(v));  var new_elements = array2.filter(v => !array1.includes(v));    console.log(missing_elements);  console.log(new_elements);


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -