Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

105
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda