difference of two arrays javascript [duplicate]












-2
















This question already has an answer here:




  • How to get the difference between two arrays in Javascript?

    60 answers




I want to compare two arrays and return a new array with any items only found in one of the two given arrays, but not both



This is my code :






function diffArray(arr1, arr2) {
var newArr = ;
for(var i =0; i <arr1.length ; i++){

if(arr2.indexOf(arr1[i]) < 0){
newArr.push(arr1[i]);
}
}
for(var j =0; j <arr2.length ; j++){

if(newArr.includes(arr2[i]) !== true){
if(arr1.indexOf(arr2[i])<0){
newArr.push(arr2[i])
}
}
}

return newArr;
}





its not working right. newArr after first for loop is empty , i cant get where i am wrong .. thank you in advance for any idea










share|improve this question















marked as duplicate by Just code, Mark Meyer javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 6:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    -2
















    This question already has an answer here:




    • How to get the difference between two arrays in Javascript?

      60 answers




    I want to compare two arrays and return a new array with any items only found in one of the two given arrays, but not both



    This is my code :






    function diffArray(arr1, arr2) {
    var newArr = ;
    for(var i =0; i <arr1.length ; i++){

    if(arr2.indexOf(arr1[i]) < 0){
    newArr.push(arr1[i]);
    }
    }
    for(var j =0; j <arr2.length ; j++){

    if(newArr.includes(arr2[i]) !== true){
    if(arr1.indexOf(arr2[i])<0){
    newArr.push(arr2[i])
    }
    }
    }

    return newArr;
    }





    its not working right. newArr after first for loop is empty , i cant get where i am wrong .. thank you in advance for any idea










    share|improve this question















    marked as duplicate by Just code, Mark Meyer javascript
    Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Jan 3 at 6:56


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      -2












      -2








      -2









      This question already has an answer here:




      • How to get the difference between two arrays in Javascript?

        60 answers




      I want to compare two arrays and return a new array with any items only found in one of the two given arrays, but not both



      This is my code :






      function diffArray(arr1, arr2) {
      var newArr = ;
      for(var i =0; i <arr1.length ; i++){

      if(arr2.indexOf(arr1[i]) < 0){
      newArr.push(arr1[i]);
      }
      }
      for(var j =0; j <arr2.length ; j++){

      if(newArr.includes(arr2[i]) !== true){
      if(arr1.indexOf(arr2[i])<0){
      newArr.push(arr2[i])
      }
      }
      }

      return newArr;
      }





      its not working right. newArr after first for loop is empty , i cant get where i am wrong .. thank you in advance for any idea










      share|improve this question

















      This question already has an answer here:




      • How to get the difference between two arrays in Javascript?

        60 answers




      I want to compare two arrays and return a new array with any items only found in one of the two given arrays, but not both



      This is my code :






      function diffArray(arr1, arr2) {
      var newArr = ;
      for(var i =0; i <arr1.length ; i++){

      if(arr2.indexOf(arr1[i]) < 0){
      newArr.push(arr1[i]);
      }
      }
      for(var j =0; j <arr2.length ; j++){

      if(newArr.includes(arr2[i]) !== true){
      if(arr1.indexOf(arr2[i])<0){
      newArr.push(arr2[i])
      }
      }
      }

      return newArr;
      }





      its not working right. newArr after first for loop is empty , i cant get where i am wrong .. thank you in advance for any idea





      This question already has an answer here:




      • How to get the difference between two arrays in Javascript?

        60 answers







      function diffArray(arr1, arr2) {
      var newArr = ;
      for(var i =0; i <arr1.length ; i++){

      if(arr2.indexOf(arr1[i]) < 0){
      newArr.push(arr1[i]);
      }
      }
      for(var j =0; j <arr2.length ; j++){

      if(newArr.includes(arr2[i]) !== true){
      if(arr1.indexOf(arr2[i])<0){
      newArr.push(arr2[i])
      }
      }
      }

      return newArr;
      }





      function diffArray(arr1, arr2) {
      var newArr = ;
      for(var i =0; i <arr1.length ; i++){

      if(arr2.indexOf(arr1[i]) < 0){
      newArr.push(arr1[i]);
      }
      }
      for(var j =0; j <arr2.length ; j++){

      if(newArr.includes(arr2[i]) !== true){
      if(arr1.indexOf(arr2[i])<0){
      newArr.push(arr2[i])
      }
      }
      }

      return newArr;
      }






      javascript arrays






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 7:30









      Ashish

      39319




      39319










      asked Jan 3 at 6:53









      engeng

      133




      133




      marked as duplicate by Just code, Mark Meyer javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Jan 3 at 6:56


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Just code, Mark Meyer javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Jan 3 at 6:56


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          1














          This works embed it into function






          let ad=[1,2,3,4,8,9];
          let b=[2,7,8,1];

          let k=ad.reduce((o,a)=>{
          if(b.indexOf(a) > -1)
          {
          o.push(a);
          }
          return o;

          },)


          console.log(k);








          share|improve this answer
























          • thank you so much , but i wanna know where am i wrong in my code

            – eng
            Jan 3 at 7:39











          • I guess the problem is with includes function ,you should check its use -w3schools.com/jsref/jsref_includes.asp .Anyway pls accept the answer if it worked for you .Thanks

            – Shubh Dixit
            Jan 3 at 8:34




















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          This works embed it into function






          let ad=[1,2,3,4,8,9];
          let b=[2,7,8,1];

          let k=ad.reduce((o,a)=>{
          if(b.indexOf(a) > -1)
          {
          o.push(a);
          }
          return o;

          },)


          console.log(k);








          share|improve this answer
























          • thank you so much , but i wanna know where am i wrong in my code

            – eng
            Jan 3 at 7:39











          • I guess the problem is with includes function ,you should check its use -w3schools.com/jsref/jsref_includes.asp .Anyway pls accept the answer if it worked for you .Thanks

            – Shubh Dixit
            Jan 3 at 8:34


















          1














          This works embed it into function






          let ad=[1,2,3,4,8,9];
          let b=[2,7,8,1];

          let k=ad.reduce((o,a)=>{
          if(b.indexOf(a) > -1)
          {
          o.push(a);
          }
          return o;

          },)


          console.log(k);








          share|improve this answer
























          • thank you so much , but i wanna know where am i wrong in my code

            – eng
            Jan 3 at 7:39











          • I guess the problem is with includes function ,you should check its use -w3schools.com/jsref/jsref_includes.asp .Anyway pls accept the answer if it worked for you .Thanks

            – Shubh Dixit
            Jan 3 at 8:34
















          1












          1








          1







          This works embed it into function






          let ad=[1,2,3,4,8,9];
          let b=[2,7,8,1];

          let k=ad.reduce((o,a)=>{
          if(b.indexOf(a) > -1)
          {
          o.push(a);
          }
          return o;

          },)


          console.log(k);








          share|improve this answer













          This works embed it into function






          let ad=[1,2,3,4,8,9];
          let b=[2,7,8,1];

          let k=ad.reduce((o,a)=>{
          if(b.indexOf(a) > -1)
          {
          o.push(a);
          }
          return o;

          },)


          console.log(k);








          let ad=[1,2,3,4,8,9];
          let b=[2,7,8,1];

          let k=ad.reduce((o,a)=>{
          if(b.indexOf(a) > -1)
          {
          o.push(a);
          }
          return o;

          },)


          console.log(k);





          let ad=[1,2,3,4,8,9];
          let b=[2,7,8,1];

          let k=ad.reduce((o,a)=>{
          if(b.indexOf(a) > -1)
          {
          o.push(a);
          }
          return o;

          },)


          console.log(k);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 6:59









          Shubh DixitShubh Dixit

          1




          1













          • thank you so much , but i wanna know where am i wrong in my code

            – eng
            Jan 3 at 7:39











          • I guess the problem is with includes function ,you should check its use -w3schools.com/jsref/jsref_includes.asp .Anyway pls accept the answer if it worked for you .Thanks

            – Shubh Dixit
            Jan 3 at 8:34





















          • thank you so much , but i wanna know where am i wrong in my code

            – eng
            Jan 3 at 7:39











          • I guess the problem is with includes function ,you should check its use -w3schools.com/jsref/jsref_includes.asp .Anyway pls accept the answer if it worked for you .Thanks

            – Shubh Dixit
            Jan 3 at 8:34



















          thank you so much , but i wanna know where am i wrong in my code

          – eng
          Jan 3 at 7:39





          thank you so much , but i wanna know where am i wrong in my code

          – eng
          Jan 3 at 7:39













          I guess the problem is with includes function ,you should check its use -w3schools.com/jsref/jsref_includes.asp .Anyway pls accept the answer if it worked for you .Thanks

          – Shubh Dixit
          Jan 3 at 8:34







          I guess the problem is with includes function ,you should check its use -w3schools.com/jsref/jsref_includes.asp .Anyway pls accept the answer if it worked for you .Thanks

          – Shubh Dixit
          Jan 3 at 8:34







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas