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

88
Visualizações
Determine how many key values have changed in an object

I have two of the same objects to compare, with different data types as object props (array and object).

I want to know how many properties are different than the first one. See example below:

OriginalObject = {
 names : [],
 numbers : [],
 dates :{
  from: null,
  to : null,
 },
 other :{
  from: null,
  to : null,
 },
}

ModifiedObject = {
 names : ['Steven', 'Judy'],
 numbers : [1,5,7],
 dates :{
  from: 15152112512,
  to : null,
 },
 other :{
  from: null,
  to : null,
 },
}

Since names, numbers, and dates changed, desired output is: 3

I was thinking perhaps I could iterate over the object properties and use _.isEqual of lodash, but I would like to know if there is an easy and shorthanded version to do achieve this.

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

If you want a solution using lodash, you could apply _.toPairs() on both of your objects. Then use _.differenceWith() to compare the obtained arrays and finally get the length of the comparison array :

OriginalObject = {
 names : [],
 numbers : [],
 dates :{
  from: null,
  to : null,
 },
 other :{
  from: null,
  to : null,
 },
}

ModifiedObject = {
 names : ['Steven', 'Judy'],
 numbers : [1,5,7],
 dates :{
  from: 15152112512,
  to : null,
 },
 other :{
  from: null,
  to : null,
 },
}

console.log(_.differenceWith(_.toPairs(OriginalObject), _.toPairs(ModifiedObject), _.isEqual).length);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>

about 4 years ago · Santiago Trujillo Relatório

0

I have used this as a solution.

 appliedFiltersCount (state) {
        let changedCount = 0
        Object.keys(state.filters).forEach((field) => {
          if (field in state.filters && field in getDefaultState().filters) {
            if (!isEqual(state.filters[field], getDefaultState().filters[field])) {
              changedCount++
            }
          }
        })
        return changedCount
 },
about 4 years ago · Santiago Trujillo 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