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

235
Visualizações
How to return the difference between two Objects having array and return the difference value dynamically

Let's say I have two objects of arrays:

const newObj = {
      description: "abcd",
      type: "anything",
      list: [
        { id: 1, name: "Peter" },
        { id: 2, name: "Cathenna" },
        { id: 3, name: "Chiyo" }
      ]
    }

   const oldObj = {
      description: "wwew",
      type: "anything",
      list: [
        { id: 1, name: "Amara" },
        { id: 2, name: "shinzo" },
        { id: 3, name: "Chiyo" }
      ]
    }

I want to find all the updated data in newObj objects. Ex, description of oldObj is updated to "abcd" in newObj and in list name of two objects has been updated. So, my expected output is:

const extractObjDiff = {
      description: "abcd",
      list: [
        { id: 1, name: "Peter" },
        { id: 2, name: "Cathenna" }
      ]
    }

I have tried below code but it's not working for array list.

function extractObjDiff(newObj, oldObj) {
  var r = {};
  for (var prop in newObj) {
    if (!oldObj.hasOwnProperty(prop)) {
      if (Array.isArray(newObj)) {
        r = newObj;
      } else {
        r[prop] = newObj[prop];
      }
    } else if (newObj[prop] === Object(newObj[prop])) {
      var difference = newObj[prop].length !== oldObj[prop].length ? newObj[prop] : extractObjDiff(newObj[prop], oldObj[prop], []);
      if (Object.keys(difference).length > 0) r[prop] = difference;
    } else if (newObj[prop] !== oldObj[prop]) {
      if (newObj[prop] === undefined)
        r[prop] = 'undefined';
      if (newObj[prop] === null)
        r[prop] = null;
      else if (typeof newObj[prop] === 'function')
        r[prop] = 'function';
      else if (typeof newObj[prop] === 'object')
        r[prop] = 'object'
      else if (Array.isArray(newObj))
        r = newObj;
      else
        r[prop] = newObj[prop];
    }
  }
  return r;
}

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

0

I have added 2 functions, first one give your expected output and second one give the every changes in the new object.

const newObj = {
  description: "abcd",
  type: "anything",
  list: [
    { id: 1, name: "Peter" },
    { id: 2, name: "Cathenna" },
    { id: 3, name: "Chiyo" }
  ]
};

const oldObj = {
  description: "wwew",
  type: "anything",
  list: [
    { id: 1, name: "Amara" },
    { id: 2, name: "shinzo" },
    { id: 3, name: "Chiyo" }
  ]
};

const findSubChanges = (oldObj, newObj) => {
  let changes=[]
  for (const i in oldObj) {
    if (oldObj[i] instanceof Object) {
      let isChange=false
      for (const j in oldObj[i]){
        if(oldObj[i][j] !== newObj[i][j]){
          isChange=true
          break
        }
      }
      if (isChange){
        changes[i] = newObj[i]
      }
    } else {
      if (oldObj[i] !== newObj[i]) {
        changes[i] = newObj[i];
      }
    }
  }
  return changes
};

const findChanges = (oldObj, newObj) => {
  let changes={}
  for (const i in oldObj) {
    if (oldObj[i] instanceof Object) {
      const change = findSubChanges(oldObj[i], newObj[i]);
      if (change) {
        changes[i] = change
      }
    } else {
      if (oldObj[i] !== newObj[i]) {
        changes[i] = newObj[i];
      }
    }
  }
  return changes
};

const extractObjDiff=findChanges(oldObj, newObj)
console.log(extractObjDiff);

const newObj = {
  description: "abcd",
  type: "anything",
  list: [
    { id: 1, name: "Peter" },
    { id: 2, name: "Cathenna" },
    { id: 3, name: "Chiyo" }
  ]
};

const oldObj = {
  description: "wwew",
  type: "anything",
  list: [
    { id: 1, name: "Amara" },
    { id: 2, name: "shinzo" },
    { id: 3, name: "Chiyo" }
  ]
};


const isArray = (item) => item instanceof Array;
const isJSONObject = (item) =>
  item instanceof Object && !(item instanceof Array);

const findEveryChanges = (oldObj, newObj) => {
  if (
    (isArray(oldObj) && isArray(newObj)) ||
    (isJSONObject(oldObj) && isJSONObject(newObj))
  ) {
    let changes = isArray(oldObj) ? [] : {};
    for (const i in oldObj) {
      if (oldObj[i] instanceof Object) {
        const change = findEveryChanges(oldObj[i], newObj[i]);
        if (change) {
          changes[i] = change;
        }
      } else {
        if (oldObj[i] !== newObj[i]) {
          changes[i] = newObj[i];
        }
      }
    }
    if (Object.keys(changes).length > 0) {
      return changes;
    }
    return;
  }
  if (oldObj !== newObj) {
    return newObj;
  }
  return;
};


console.log(findEveryChanges(oldObj, newObj));

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