I Ingin membandingkan dua objek yang hampir selalu memiliki properti yang sama dan membuat objek baru berdasarkan properti yang memiliki nilai yang berbeda dari objek pertama. dalam contoh di bawah ini saya ingin memeriksa apakah inputData memiliki key:values ​​yang berbeda dari currentData. kemudian buat objek baru berdasarkan kunci dan nilai ini. Semoga masuk akal? pendekatan apa yang harus saya miliki? beberapa Oject.assign rekursif? operasi penyebaran? untuk lingkaran?

const currentData = {
  title: 'John',
  email: 'info@abc.com'
  address {
    street: 'myStreet 13'  
  }
}

const inputData = {
  title: 'Tom',
  email: 'info@abc.com',
  address {
    street: 'yourStreet 17'  
  }
}

Hasilnya akan seperti ini:

const result = {
  title: 'Tom',
  address: {
    street: 'yourStreet 17'
  }   
}
0
csk87 28 Oktober 2019, 15:02

1 menjawab

Jawaban Terbaik

Anda akan memerlukan fungsi rekursif yang mengulang objek, sesuatu di sepanjang baris ini:

function compareObjects(a, b) {
    // Assume they'll be the same
    let result = null;
    // Keep track of the keys we've seen
    let keys = new Set();
    for (const key in a) {
        keys.add(key);
        if (!(key in b)) {
            // You'll want to decide what to do here, use `undefined`?
        } else {
            const avalue = a[key];
            const bvalue = b[key];
            if (avalue !== bvalue) {
                const aIsObject = typeof avalue === "object";
                const bIsObject = typeof bvalue === "object";
                if (aIsObject && bIsObject) {
                    // Both are objects, recurse
                    const update = compareObjects(avalue, bvalue);
                    if (update) {
                        result = result || {};
                        result[key] = update;
                    }
                } else {
                    // Different values
                    result = result || {};
                    result[key] = bvalue;
                }
            }
        }
    }
    // Add in any that are in `b` but weren't in `a`
    for (const key in b) {
        if (!keys.has(key)) {
            result = result || {};
            result[key] = b[key];
        }
    }
    return result;
}

Contoh Langsung:

const currentData = {
    title: 'John',
    email: 'info@abc.com',
    address: {
        street: 'myStreet 13'  
    }
};

const inputData = {
    title: 'Tom',
    email: 'info@abc.com',
    address: {
        street: 'yourStreet 17'  
    }
};

function compareObjects(a, b) {
    // Assume they'll be the same
    let result = null;
    // Keep track of the keys we've seen
    let keys = new Set();
    for (const key in a) {
        keys.add(key);
        if (!(key in b)) {
            // You'll want to decide what to do here, use `undefined`?
        } else {
            const avalue = a[key];
            const bvalue = b[key];
            if (avalue !== bvalue) {
                const aIsObject = typeof avalue === "object";
                const bIsObject = typeof bvalue === "object";
                if (aIsObject && bIsObject) {
                    // Both are objects, recurse
                    const update = compareObjects(avalue, bvalue);
                    if (update) {
                        result = result || {};
                        result[key] = update;
                    }
                } else {
                    // Different values
                    result = result || {};
                    result[key] = bvalue;
                }
            }
        }
    }
    // Add in any that are in `b` but weren't in `a`
    for (const key in b) {
        if (!keys.has(key)) {
            result = result || {};
            result[key] = b[key];
        }
    }
    return result;
}

console.log(compareObjects(currentData, inputData));
1
T.J. Crowder 28 Oktober 2019, 12:12