The only way you should be comparing two objects in JavaScript

preview_player
Показать описание
#coding #js #object #array #es6
Рекомендации по теме
Комментарии
Автор

The example I showed in the video isn't great, because we could just compare two responses as strings / jsons with the same result. I hope that you also noticed that ;)

invertcode
Автор

function equal(a, b) {
if (!a || !b || typeof a !== "object" || typeof b !== "object") return a === b;

const aProps =
const bProps =

if (aProps.length !== bProps.length) return false;

for (const prop of aProps) {
if (typeof a[prop] === "object") {
if (!equal(a[prop], b[prop])) return false;
} else if (typeof a[prop] === "function") {
if ((!a[prop] || !b[prop]) && a[prop] !== b[prop]) return false;
if (a[prop].toString() !== b[prop].toString()) return false;
} else if (a[prop] !== b[prop]) return false;
}

return true;
}

invertcode