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

106
Visualizações
Javascript - compare two array of objects based on their length

I have 2 array of objects and 2 if conditions

    if(JSON.stringify(this.updatedData) !== JSON.stringify(this.originalData) && this.updatedData.length === this.originalData.length) {            
        this.customAttributes = editedCustomAttributes;
        this.customAttributes.forEach(function (element) {
          element.action = "edit";
        });
    }
      
    if (JSON.stringify(this.updatedData) !== JSON.stringify(this.originalData) && his.updatedData.length !== this.originalData.length) {
        const items = this.updatedData.filter(item => item.attributeId == null);
        items.forEach(function (element) {
          element.action = "add";
        });
    }

While editing a grid row, originalData and updatedData will have the same length hence the first if condition executes. While I am adding new row, updatedData length will be more than originalData hence the 2nd if condition executes.

But I have a situation where a user can add a new row and edit an existing row at once. At that particular scenario, I need to execute both the if conditions and I stuck there. Any suggestions will be appretiated. Thanks.

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

0

You could try something as follows:

function check() {
  const original = JSON.stringify(originalData)

  if (originalData.length !== updatedData.length) {
    // If lengths are not equal then new rows were added
    addedAction()

    // Check the two arrays again without those new rows
    const newRows = updatedData.length - originalData.length
    if (JSON.stringify(updatedData.slice(0, -newRows)) !== original) {
      editedAction()
    }

  } else if (JSON.stringify(updatedData) !== original) {
    editedAction()
  }

}

The addedAction() and editedAction() functions are just what will happen in response to an edit or addition.

In the case where an addition and an edit is performed addedAction() will be executed first due to diffirence in lengths. Then originalData will be checked against a portion of the updatedData and the editedAction() function will be executed if they are not the same after removing the newly added rows.

Here is an example with the three cases:

function check() {
  const original = JSON.stringify(originalData)

  if (originalData.length !== updatedData.length) {
    // If lengths are not equal then new rows were added
    addedAction()

    // Check the two arrays again without those new rows
    const newRows = updatedData.length - originalData.length
    if (JSON.stringify(updatedData.slice(0, -newRows)) !== original) {
      editedAction()
    }

  } else if (JSON.stringify(updatedData) !== original) {
    editedAction()
  }

  console.log('')
}

function editedAction() {
  console.log('Edited')
}

function addedAction() {
  console.log('Added')
}

console.log('First case:')
let originalData = [{ name: 'a' }, { name: 'b' }]
let updatedData = [{ name: 'a' }, { name: 'b' }, { name: 'd' }, { name: 'asd' }]
check()

console.log('Second case:')
originalData = [{ name: 'a' }, { name: 'b' }]
updatedData = [{ name: 'a' }, { name: 'TEST' }]
check()

console.log('Third case:')
originalData = [{ name: 'a' }, { name: 'b' }]
updatedData = [{ name: 'a' }, { name: '123' }, { name: 'test2' }]
check()
.as-console-wrapper { min-height: 100% }

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