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

98
Visualizações
Convert some object keys to strings and then compare new values with old ones

I'm trying to work with data that resembles this kind of object:

const sampleData = {
  coyotes: '',
  armadillos: false,
  wombats: [''],
  geckos: 0,
  dogs: {
    beagle: '',
    bulldog: ''
  },
  wolves: [
    {
      type: '',
      color: '',
      range: '',
      status: {
        endangered: true,
        protected: false
      }
    }
  ],
  cats: {}
}

As you can see, it's got a mix of different JavaScript types: strings, Booleans, numbers, etc.

When something in the form changes, I need to compare the old object with the changed object and check for equality, among other things. To determine if something has changed, I use this recursive function:

export const getChangedValues = (initialValues, values) => {
  return Object
    .entries(values)
    .reduce((acc, [key, value]) => {
      const hasChanged = initialValues[key] !== value
      if (hasChanged) {
        acc[key] = value
      }
      return acc
    }, {})
}

However, when I compare new objects with old using lodash isEqual, I get a false for the number values because our fields save numbers as strings (I have no control over that).

So, I use this to convert JUST the numbers in the object to strings, because I need to leave everything else alone (especially the Booleans):

export const convertValuesToStrings = (obj) => {
  return _.cloneDeepWith(obj, value => {
    return (_.isNumber(value)) ? _.toString(value) : undefined
  })
}

This works perfectly in converting numbers to strings, but I'm getting a false positive for the nested objects. After running the string converter and then checking if there has been a change, it's also returning objects, even if their underlying data hasn't changed.

I'm still learning the more complex areas of JavaScript, like recursion. Can anyone spot what I'm missing here?

Bottom line is that I need to recursively compare two objects after converting any number values (and ONLY number values) to strings. :)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

To keep this simple, have you tried comparing the objects using JSON.stringify()?

if(JSON.stringify(oldObject) === JSON.stringify(newObject)) {
   console.log('objects are basically equal' ) 
}

This approach is pretty simple for fully comparing a deep object. This cannot be modified too much but solves the problem using a tested built in function and let's you focus on writing the actual application.

about 4 years ago · Juan Pablo Isaza Relatório
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