admin管理员组

文章数量:1400092

Lets say I have an array with following structure inside:

applications = [
    {
        source : {},
        highlight : []
    },
    {
       source : {},
       highlight : []
    },

    // More objects with same structure as objects above. 
]

Sometime, the array structure can be like following:

  applications = [
    {
        source : {}
    },
    {
       source : {}
    },

    // More objects with same structure as objects above. 
]

«N.B.» source and highlight have content, but now shown here.

Lets further say that I want to pare the two arrays above(with content). I want to specific check if they are equal.

A famous function for doing it, is this one:

arraysEqual(arr1, arr2) {
    if (arr1.length !== arr2.length)
        return false;
    for (var i = arr1.length; i--;) {
        if (arr1[i] !== arr2[i])
            return false;
    }
    return true;
}

I want to be sure, if this function will help me to decide whether the arrays are equal or not?

Lets say I have an array with following structure inside:

applications = [
    {
        source : {},
        highlight : []
    },
    {
       source : {},
       highlight : []
    },

    // More objects with same structure as objects above. 
]

Sometime, the array structure can be like following:

  applications = [
    {
        source : {}
    },
    {
       source : {}
    },

    // More objects with same structure as objects above. 
]

«N.B.» source and highlight have content, but now shown here.

Lets further say that I want to pare the two arrays above(with content). I want to specific check if they are equal.

A famous function for doing it, is this one:

arraysEqual(arr1, arr2) {
    if (arr1.length !== arr2.length)
        return false;
    for (var i = arr1.length; i--;) {
        if (arr1[i] !== arr2[i])
            return false;
    }
    return true;
}

I want to be sure, if this function will help me to decide whether the arrays are equal or not?

Share Improve this question asked Dec 20, 2016 at 10:05 Jagjit SinghJagjit Singh 7519 silver badges19 bronze badges 1
  • the function checks whether the two length are equals and, if so, if each element of the array equals the other, it will return true. That might make sense, though it will miss a case where objects order is not the same, but the content really is, like this case: jsfiddle/4ke1gdz0 – briosheje Commented Dec 20, 2016 at 10:15
Add a ment  | 

3 Answers 3

Reset to default 5

Here's the simple way...

JSON.stringify(arr1) == JSON.stringify(arr2)

The issue with the arraysEqual function you identify is that it would only work if the items in arr1 and arr2 were type String or Number (or Boolean), ie { source: 1 } is != { source: 1 }, because although they have the same content, they are different Objects.

Using Lodash method _.isEqual could be an option:

var object = { 'a': 1 };
var other = { 'a': 1 };

_.isEqual(object, other);
// => true

object === other;
// => false

Benefits:

  • Trustable
  • Makes a deep parison
  • If you don't want to load the whole library you can add just this method.

I don't like to use Lodash for everything and do it in Vanilla JS in general, but in these cases I think it's worth using a library.

If you are talking about a shallow equal, then the function could be used (e.g. when paring arrays like ['a', 4, 'b']).

This is however not likely what you want because of the data inside the arrays which consists (according to your example) objects and arrays.

If you want the data from each array to be exactly the same, you need a deep equal (i.e. pare all values, objects and arrays in depth, recursively). You could implement this yourself, but if possible use a third party library like underscore.

本文标签: JavascriptCheck if two arrays are equal when arrays contain an object and an arrayStack Overflow