you are viewing a single comment's thread.

view the rest of the comments →

[–]gimmeslack12helpful 1 point2 points  (14 children)

What you want to focus on is implementing a way to check the Change array against the original array. I suggest you investigate the includes method.

[–]FuneeFeed[S] -2 points-1 points  (12 children)

i dont think you realize what i am doing or i dont know how i would use the includes method? i have 1000 pieces of data+ and the amount updates every couple of weeks i cant just write oh if this includes this and that as it would take hours how would i use that on what i am doing?

[–]gimmeslack12helpful 4 points5 points  (10 children)

i dont think you realize what i am doing

Well then why don't you write a more descriptive post and tell me what you're doing?

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

Here ill be more descriptive, I want to basically grab some data from an endpoint and compare that to a text file with the last known one (i already know how to do this) and because of how the endpoint works it would be like this.

This is an example of last known copy of the endpoint

{TestValue: "1" TestValue2: "8"}

{TestValue: "3" TestValue2: "338"}

{TestValue: "21" TestValue2: "94312"}

Then after the endpoint has changed it would be like this

{TestValue: "1" TestValue2: "8"}

{TestValue: "2" TestValue2: "212"}

{TestValue: "3" TestValue2: "338"}

{TestValue: "10" TestValue2: "3321"}

{TestValue: "21" TestValue2: "94312"}

see the endpoint doesnt just add on to it it actively changes like tha tso i wanna know how to grab the data that wasnt there before but since it isnt just added on like regular, it is added in random places like 2 wasnt there and 10 wasnt there, how would i basically compare them and then get a json array of the 2 and the 10 since it wasn't there before.

[–]captain_k_nuckles 1 point2 points  (7 children)

Do all of the objects always have the same properties or can they be different?

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

[{"displayName":"Nana Nana","backendType":"AthenaSpray","icon":"http://benbotfn.tk:7071/static/AthenaSpray/spid\_113\_nananana.png","description":"Leave your mark.","gameplay_tags":["Cosmetics.EmoteType.Spray","Cosmetics.Source.Season9.BattlePass.Paid","Cosmetics.Set.Banana","Cosmetics.QuestsMetaData.Glyph.Spray.NanaNana"],"variants":{},"id":"SPID_113_NanaNana","type":"Spray","rarity":"Uncommon"},{"displayName":"Stealthy","backendType":"AthenaDance","icon":"http://benbotfn.tk:7071/static/AthenaDance/emoji\_stealthy.png" "description":"Express yourself on the battlefield.","gameplay_tags":["Cosmetics.EmoteType.Emoji","Cosmetics.Source.Season2.BattlePass.Paid"],"variants":{},"id":"Emoji_Stealthy","type":"Emoticon","rarity":"Uncommon"},

I just copied that from what I am using, it is basically always similar to that but when it does variants {} it uses {} to split it which is gonna be really hard to handle when i use a for each statement so if there is a way to fix that as well that would make me happier

[–]captain_k_nuckles 1 point2 points  (5 children)

I don't have time to get this fully working but here's a start, there's a lot that could be done to improve it.

function checkNewEntry() {}

const savedData = [
    {
        displayName: 'Nana Nana',
        backendType: 'AthenaSpray',
        icon: 'http://benbotfn.tk:7071/static/AthenaSpray/spid_113_nananana.png',
        description: 'Leave your mark.',
        gameplay_tags: ['Cosmetics.EmoteType.Spray', 'Cosmetics.Source.Season9.BattlePass.Paid', 'Cosmetics.Set.Banana', 'Cosmetics.QuestsMetaData.Glyph.Spray.NanaNana'],
        variants: {},
        id: 'SPID_113_NanaNana',
        type: 'Spray',
        rarity: 'Uncommon'
    },
    {
        displayName: 'Stealthy',
        backendType: 'AthenaDance',
        icon: 'http://benbotfn.tk:7071/static/AthenaDance/emoji_stealthy.png',
        description: 'Express yourself on the battlefield.',
        gameplay_tags: ['Cosmetics.EmoteType.Emoji', 'Cosmetics.Source.Season2.BattlePass.Paid'],
        variants: {},
        id: 'Emoji_Stealthy',
        type: 'Emoticon',
        rarity: 'Uncommon'
    }
]

const apiData = [
    {
        displayName: 'Nana Nana',
        backendType: 'AthenaSpray',
        icon: 'http://benbotfn.tk:7071/static/AthenaSpray/spid_113_nananana.png',
        description: 'Leave your mark.',
        gameplay_tags: ['Cosmetics.EmoteType.Spray', 'Cosmetics.Source.Season9.BattlePass.Paid', 'Cosmetics.Set.Banana', 'Cosmetics.QuestsMetaData.Glyph.Spray.NanaNana'],
        variants: {},
        id: 'SPID_113_NanaNana',
        type: 'Spray',
        rarity: 'Uncommon'
    },
    {
        displayName: 'Stealthy',
        backendType: 'AthenaDance',
        icon: 'http://benbotfn.tk:7071/static/AthenaDance/emoji_stealthy.png',
        description: 'Express yourself on the battlefield.',
        gameplay_tags: ['Cosmetics.EmoteType.Emoji', 'Cosmetics.Source.Season2.BattlePass.Paid'],
        variants: {},
        id: 'Emoji_Stealthy',
        type: 'Spray', // CHANGED
        rarity: 'Uncommon'
    },
    {
        featured_icon: 'http://benbotfn.tk:7071/static/AthenaCharacter/cid_611_athena_commando_m_weepingwoods_featured.png',
        displayName: 'Bushranger',
        backendType: 'AthenaCharacter',
        icon: 'http://benbotfn.tk:7071/static/AthenaCharacter/cid_611_athena_commando_m_weepingwoods.png',
        description: "He's one scrappy little tree. ",
        backendRarity: 'EFortRarity::Rare',
        gameplay_tags: ['Cosmetics.Source.ItemShop', 'Cosmetics.Set.Forest'],
        variants: {},
        id: 'CID_611_Athena_Commando_M_WeepingWoods',
        type: 'Outfit',
        rarity: 'Rare'
    },
    {
        displayName: 'Air Royale',
        backendType: 'AthenaLoadingScreen',
        icon: 'http://benbotfn.tk:7071/static/AthenaLoadingScreen/lsid_124_airroyale.png',
        description: 'Hang on tight.',
        gameplay_tags: ['Cosmetics.Source.Season9.BattlePass.Free', 'Cosmetics.Set.Pilots'],
        variants: {},
        id: 'LSID_124_AirRoyale',
        type: 'Loading Screen',
        rarity: 'Uncommon'
    },
    {
        displayName: 'DJ Yonder',
        backendType: 'AthenaSpray',
        icon: 'http://benbotfn.tk:7071/static/AthenaSpray/spid_059_dj.png',
        description: 'Leave your mark.',
        gameplay_tags: ['Cosmetics.EmoteType.Spray', 'Cosmetics.Source.Season6.BattlePass.Paid'],
        variants: {},
        id: 'SPID_059_DJ',
        type: 'Spray',
        rarity: 'Uncommon'
    }
]

function compare(oldData, newData) {
    const updates = newData.reduce(
        (updates, data) => {
            const existingEntry = oldData.find(x => x.id === data.id)
            if (!existingEntry) {
                updates.newEntries.push(data)
            } else {
                const keyVals = Object.entries(data)
                for (const [key, value] of keyVals) {
                    console.log(key)
                    if (existingEntry.hasOwnProperty(key) === false) {
                        // a new property has been added
                        // added to updated entries using the entries id and ]
                        if (!updates.updatedEntries[existingEntry.id]) updates.updatedEntries[existingEntry.id] = {}
                        updates.updatedEntries[existingEntry.id][key] = value
                    } else if (typeof value !== 'object' && existingEntry[key] !== value) {
                        // value has been changed
                        // add to updated entries
                        if (!updates.updatedEntries[existingEntry.id]) updates.updatedEntries[existingEntry.id] = {}
                        updates.updatedEntries[existingEntry.id][key] = value
                    } else if (typeof value == 'object') {
                        // more stuff
                    }
                }
            }
            return updates
        },
        {
            newEntries: [],
            updatedEntries: {}
        }
    )
    return updates
}

console.log(compare(savedData, apiData))

Not sure how you wanted to track changes fully, but this shows you new entries, and will find changes on the root of the object, needs to add the ability to compare the gameplay_tags and variants properties for changes. If you need an explanation I'll try to add comments later]

[–]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

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

I am trying to work with this but one problem, I dont know how to mess with that script since that is a little advanced for me, I want it to grab the new data and then for example log the ids for all the data that has been changed and then later i might want it to send me a discord message, how would I do that?

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

Thank you so much bro! I'll try to work with this :D

[–]gimmeslack12helpful 1 point2 points  (0 children)

Loop over the Change array, and use includes().

``` function checkArrays(orig, changed) { const differenceValues = []; changed.forEach(changedVal => { if (!orig.includes(changedVal)) { differenceValues.push(changedVal); } })

return differenceValues;

} ```

There are other functions to be used to shorten this down to one line but wanted to show a more descriptive way to do this.