Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda