Is there a way to check if two arrays have the same elements?

前端 未结 4 1921
名媛妹妹
名媛妹妹 2021-01-18 04:56

Let say I have 2 arrays

firstArray  = [1, 2, 3, 4, 5];
secondArray = [5, 4, 3, 2, 1];

I want to know if they contain the same elements, whi

4条回答
  •  日久生厌
    2021-01-18 05:40

    Well there is an Array.sort() method in JavaScript, and for comparing the (sorted) arrays, I think it's best to check out this question, as it is has a really good answer.

    Especially note that comparing arrays as strings (e.g. by JSON.stringify) is a very bad idea, as values like "2,3" might break such a check.

提交回复
热议问题