Compare two Arrays Javascript - Associative

后端 未结 3 542
醉梦人生
醉梦人生 2021-01-17 22:30

I have searched on here for a quality method to compare associative arrays in javascript. The only decent solution I have found is the PHP.JS project which has some comparat

3条回答
  •  没有蜡笔的小新
    2021-01-17 23:05

    This is an old question, but since it comes up first in a google search for comparing arrays, I thought I would throw in an alternative solution that works even when the array has two different objects with the same values.

    function arrays_equal(a, b) {
        return JSON.stringify(a) == JSON.stringify(b);
    }
    

    Note: This is order dependent, so if order doesn't matter, you can always do a sort ahead of time.

提交回复
热议问题