So sánh hai array trong javascript

array,tipjs

Làm thế nào để đánh giá xem hai mảng có bằng nhau không? Có hai cách đó là sử dụng JSON.stringify() hoặc toString(). Nhưng trong hai cách này có một số nguy cơ tiềm ẩn, như là 1 với '1'. Ta có cách này hay hơn.

function equar(a, b) {
    if (a.length !== b.length) {
        return false
    } else {
        for (let i = 0; i < a.length; i++) {
            if (a[i] !== b[i]) {
                return false
            }
        }
        return true;
    }
}
var s = equar([1, '2', 3], [1, 2, 3]);
var t = equar([1, 2, 3], [1, 2, 3]);
console.log(s);  //  false
console.log(t);  //  true

Có thể bạn đã miss một số snippets code

Callback in javascript

Callback in javascript

Extend Object javascript

Extend Object javascript

Ajax, call jQuery POST to node.js expressjs

Ajax, call jQuery POST to node.js expressjs

Copy a string to the clipboard

Copy a string to the clipboard. Only works as a result of user action (i.e. inside a click event listener).

group by property javascript use reduce

group by property javascript use reduce