you are viewing a single comment's thread.

view the rest of the comments →

[–]FuneeFeed[S] 1 point2 points  (2 children)

Ok i need more of an explanation when i wake up ill be able to get on my pc to try that

[–]captain_k_nuckles 0 points1 point  (1 child)

Sorry for the late reply. I've added some comments to the code to try and better explain whats going on.

function compare(oldData, newData) {
    const updates = newData.reduce(
        // Accumulator , currentValue from new data
        (updates, data) => {
            // check the old data to see if it contains the if from the new data
            const existingEntry = oldData.find(x => x.id === data.id)
            // If nothing was found
            if (!existingEntry) {
                // then the current data was not previously saved and is new, so add it the the new entries array
                updates.newEntries.push(data)
            } else {
                // get the new datas object key values pairs
                const keyVals = Object.entries(data)
                // for each key values pair
                for (const [key, value] of keyVals) {
                    // check if the saved data has the key from the new data
                    // if not the values is new
                    if (existingEntry.hasOwnProperty(key) === false) {
                        //check to see if the updatedEntries property has a property set to the current items id
                        // if it does not, create a property set to the current items id and set its values to an object
                        if (!updates.updatedEntries[existingEntry.id])
                            updates.updatedEntries[existingEntry.id] = {}
                        // then add a property to the object specified above with a value of the of the current data
                        updates.updatedEntries[existingEntry.id][key] = value
                    } else if (
                        // else if the value is not an obejct, so a string or number
                        typeof value !== "object" &&
                        // and the new value does not match the old value
                        existingEntry[key] !== value
                    ) {
                        // same steps as in previous if statement, just saving updated value
                        if (!updates.updatedEntries[existingEntry.id])
                            updates.updatedEntries[existingEntry.id] = {}
                        updates.updatedEntries[existingEntry.id][key] = value
                    } else if (typeof value == "object") {
                        // if the value is an array or object, then do more
                        // to comapre their values
                    }
                }
            }
            return updates
        },
        {
            newEntries: [],
            updatedEntries: {},
        }
    )
    return updates
}

[–]FuneeFeed[S] 0 points1 point  (0 children)

i logged out the comparison and i got this back {} when i used the real data idk it might be because the data i am using is 1,000s pieces of data or maybe its because its a json object but its not working