Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
How to compare object to object from file in node?

I'm trying to compare an object that im receiving from a socket.io event, against an object thats in a file on my computer. I have most of it setup, i can get it to save to the file when it finds new data, however, the data seems to be string overlapping and im not sure why/how(see picture)

enter image description here

I tried reading this https://dmitripavlutin.com/how-to-compare-objects-in-javascript/ to see if it was an issue with reading the file, or actually comparing the two objects to see which item is new (which would be coming from the socket.io data)

        console.log(collection)
        const test = new Promise((resolve,reject) => {
            fs.access(collection, fs.F_OK, function (err) { //initial file access
                if (!err) {
                    fs.readFile(collection, 'utf8', (err, data) => { // read file for data,
                        if (err) throw err;
                        var dat = JSON.parse(data) 
                        var found = []
                          for(i in dat){
                              found.push(JSON.stringify(dat[i])) // This is the data that already exists, this is coming from the file.
                          }
                          
                          resolve(found)
                        // resolve
                    })
                }
            })
        })
            
    test.then(function(data){
        //console.log(data) // This is the data from above
        for(i in NFTCollection){ // NFTData that was just recieved from the https request (sent from socket.io)
            var item = JSON.stringify(NFTCollection[i]) // This is each new item
            var target = client.channels.cache
            //console.log(data)
            if(!data.includes(item)){ // This is where i have the issue, how do i compare the object to the object thats in the file?
                console.log("Found item! " + item) 
                var parsedItem = JSON.parse(item)
                data.push(JSON.parse(item))
            }
         
            //console.log(item)
        }
        fs.writeFileSync(collection,JSON.stringify(data)) // saves the data from, but it keeps overlapping? 
        })
    })
}) 

I commented in my code what each step is trying to accomplish, the issue is at the end. when im comparing if the items from the socket.io data, exist within the data that is the node.fs file.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!