# Compare two arrays & save the mismatched data in an array

**URL:** <https://community.postman.com/t/compare-two-arrays-save-the-mismatched-data-in-an-array/40780>\
**Category:** Help Hub\
**Tags:** scripts, array, loop\
**Created:** [November 4, 2022, 12:03pm UTC](https://community.postman.com/t/compare-two-arrays-save-the-mismatched-data-in-an-array/40780 "2022-11-04T12:03:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aksh-s-rathod](https://avatars.discourse-cdn.com/v4/letter/a/e68b1a/32.png) [@aksh-s-rathod](https://community.postman.com/u/aksh-s-rathod)\
**Post date:** [November 4, 2022, 12:03pm UTC](https://community.postman.com/t/compare-two-arrays-save-the-mismatched-data-in-an-array/40780/1 "2022-11-04T12:03:56Z")

</div>

I need to compare two arrays & get the mismatched data from both into third array.

Details: following is the code I used for reference

```auto
var array1 = [1, 2, 3, 4, 8, 5, 6, 7];
var array2 = [6, 5, 4, 3, 2, 1] ;
var array3 = [];

for(var j=0; j<array1.length; j++)
{
    var ar1 = array1[j];
    //console.log(" *****"+ar1+"*****")

    for(var k=0; k<array2.length; k++)
    {
        var ar2 = array2[k];
    //console.log("####"+ar2+"####")

        if(ar1===ar2)
        {
            console.log(ar1+" is equal to "+ar2)
            break;
        }
        // else
        // if(k=array2.length)
        // {
        // console.log("the value "+ar1+" is not found")
        // }
       
    }
}

```

Please let me know if we can get mismatched data using this, else if you have any another way to compare & get mismatched data please provide the solution, Thanks !!!

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [November 4, 2022, 5:13pm UTC](https://community.postman.com/t/compare-two-arrays-save-the-mismatched-data-in-an-array/40780/2 "2022-11-04T17:13:27Z")

</div>

The problem is that you are comparing one array to another. Therefore, you need to know which array has the most entries for the comparison. There are a lot of assumptions going on here, but something like the following…

```auto
var array1 = [1, 2, 3, 4, 8, 5, 6, 7];
console.log(array1);
var array2 = [6, 5, 4, 3, 2, 1];
console.log(array2);

if (array2.length >= array1.length) {
    var difference = array2.filter(n=>!array1.includes(n))
} else {
    var difference = array1.filter(n=>!array2.includes(n))
}

console.log(difference);

```

![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/d/cd8a8f0c30b55a3113e6147b773b9dbf524ff1ed.png)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [September 27, 2023, 2:30pm UTC](https://community.postman.com/t/compare-two-arrays-save-the-mismatched-data-in-an-array/40780/3 "2023-09-27T14:30:27Z")

</div>

Closed on September 27, 2023, 2:30pm UTC

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
