Last active
April 3, 2020 07:57
-
-
Save JetLua/5fb7255d881005115727b5c4cb782ca5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {prototype: {toString}, keys} = Object | |
const a = { | |
name: 'JetLu', | |
other: {age: 12, gender: 0, alias: 2} | |
} | |
const b = { | |
name: 'JetLu', | |
other: {age: 12, gender: 0, alias: 2} | |
} | |
console.log(equal(a, b)) | |
function isObject(v) { | |
return toString.call(v).includes('Object') | |
} | |
function isArray(v) { | |
return toString.call(v).includes('Array') | |
} | |
function sameType(a, b) { | |
return toString.call(a) === toString.call(b) | |
} | |
function equal(a, b) { | |
const queue = [[a, b]] | |
while(queue.length) { | |
const [m, n] = queue.pop() | |
if (!sameType(m, n)) return | |
if (isObject(m) && keys(m).length === keys(n).length) { | |
for (const k in m) { | |
if (!sameType(m[k], n[k])) return | |
if (isObject(m[k])) queue.push([m[k], n[k]]) | |
else if (isArray(m[k])) queue.push([m[k], n[k]]) | |
else if (m[k] !== n[k]) return | |
} | |
} else if (isArray(m) && m.length === n.length) { | |
for (let k = 0; k < m.length; k++) { | |
if (!sameType(m[k], n[k])) return | |
if (isObject(m[k])) queue.push([m[k], n[k]]) | |
else if (isArray(m[k])) queue.push([m[k], n[k]]) | |
else if (m[k] !== n[k]) return | |
} | |
} else if (m !== n) return | |
} | |
return true | |
} |
厉害啊,飞哥
也就还行
飞哥可以的
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
厉害啊,飞哥