javascript - Showing identical elements of two arrays in a separate array -


i quite new javascript. trying achieve here put identical elements of 2 array array.i delete elements in original 2 arrays.

however, separate array not show identical ones.also, 2 arrays still show identical elements. not sure went wrong.

the following code may have syntax errors. had modify make easier ask.

var finalselective = ["cs348", "cs353", "cs381", "cs422", "cs448", "cs490-es0", "cs490-dso"]; var finalseelective = ["cs348", "cs352", "cs353", "cs354", "cs381", "cs422", "cs448", "cs456", "cs473", "cs490-dso", "cs490-es0"]; var seelecselec = []; //fulfills se elective , s elective. (var = 0; < finalselective.length; i++) { //there wrong one.   (var j = 0; j < finalseelective.length;j++){ //it not show correct repeats.     if (finalselective[i] == finalseelective[j]) {       seelecselec.push(finalseelective[j]);       var x = finalselective.indexof(finalselective[i]);       if (x != -1) {         finalselective.splice(x,1);       }       x = finalseelective.indexof(finalseelective[j]);       if (x != -1) {         finalseelective.splice(x,1);       }     }   } } 

here potential solution:

jsbin logging

var = ["cs348", "cs353", "cs381", "cs422", "cs448", "cs490-es0", "cs490-dso"]; var b = ["cs348", "cs352", "cs353", "cs354", "cs381", "cs422", "cs448", "cs456", "cs473", "cs490-dso", "cs490-es0"];  var c = a.concat(b).filter(function(el) {   return a.indexof(el) > -1 && b.indexof(el) > -1; }); 

edit: per comment below, code actual desired output is:

for (var = 0; < a.length; i++) {   var indexinb = b.indexof(a[i]);   if (indexinb > -1){     output.push(a[i]);     a.splice(i, 1);     b.splice(indexinb, 1);     i--;   } } 

jsbin code above only


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -