-
array.equals : 배열끼리의 비교JavaScript/입 속의 검은 잎 2022. 9. 25. 20:47
https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript
How to compare arrays in JavaScript?
I'd like to compare two arrays... ideally, efficiently. Nothing fancy, just true if they are identical, and false if not. Not surprisingly, the comparison operator doesn't seem to work. var a1 = [...
stackoverflow.com
미확인
if(Array.prototype.equals) //attach the .equals method to Array's prototype to call it on any array Array.prototype.equals = function (array) { // if the other array is a falsy value, return if (!array) return false; // compare lengths - can save a lot of time if (this.length != array.length) return false; for (var i = 0, l=this.length; i < l; i++) { // Check if we have nested arrays if (this[i] instanceof Array && array[i] instanceof Array) { // recurse into the nested arrays if (!this[i].equals(array[i])) return false; } else if (this[i] != array[i]) { // Warning - two different object instances will never be equal: {x:20} != {x:20} return false; } } return true; } // Hide method from for-in loops Object.defineProperty(Array.prototype, "equals", {enumerable: false});
'JavaScript > 입 속의 검은 잎' 카테고리의 다른 글
map(parseInt) 가 NaN 으로 응답했을 때, 이 문제는 모종의 유적(類的) 문제틀로 인한 파생이 아닐까 하는... (1) 2022.09.26 만날 찾아봤다 까먹는 것1 : 숫자 쪼개서 배열 만들기 (0) 2022.09.26 찾기 세트 : find(), findIndx(), indexOf (0) 2022.09.25 filter() (1) 2022.09.25 map() (1) 2022.09.25