javascript - Sort/intersect two arrays into two new ones. -
this question has answer here:
- javascript array difference 53 answers
let's assume have 2 lists. 1 existing names , new 1 maybe new names, deleted names or still same names.
var currentlist = ['daniel', 'lara', 'horst']; var newlist = ['mario', 'lara']; // expected result todelete = ['daniel', 'horst']; toadd = ['mario']; at end need 2 arrays contains new names , can deleted. names appear in both array can ignored.
i don't know call type of "sorting", buzzword help. notice don't have jquery @ all.
thanks in advance.
thats pretty straightforward, 2 loops:
var currentlist = ['daniel', 'lara', 'horst']; var newlist = ['mario', 'lara']; var todelete = []; var toadd = []; for(var i=0;i<newlist.length;i++){ if(currentlist.indexof(newlist[i]) >-1) toadd.push(newlist[i]) } for(var i=0;i<currentlist.length;i++){ if(newlist.indexof(currentlist[i]) == -1) todelete.push(currentlist[i]); } console.log('dele',todelete); console.log('add',toadd); live example: http://jsfiddle.net/r6l7lpel/
Comments
Post a Comment