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

168
Visualizações
How to compare the sameness of object entries of 2 arrays and how to create a merger of both objects with custom properties of the found same object?

I am trying to compare 2 objects by their property and the values Strictly using forloop. If the value of the "name" or another property matches up with each other, I want to push the property and value to value3.

Once value3 is logged, I want the response like this:

{
name: 'dog',
surname: 'good',
skills: 'programming',
age: '22'
},
{
name: 'cat',
surname: 'soft',
skills: 'engineer'
age: '12'
},
{
name: 'elephant',
surname: 'big',
skills: 'programming'
age: '23'
}

Here is the code:

var values1 = [
    {
    name: 'dog',
    surname: 'good',
    skills: 'programming'
    },
    {
    name: 'cat',
    surname: 'soft',
    skills: 'engineer'
    },
    {
    name: 'elephant',
    surname: 'big',
    skills: 'programming'
    }
]

var values2 = [
    {
    name: 'cat',
    food: 'fish',
    age: '12'
    },
    {
    name: 'elephant',
    food: 'leafs',
    age: '13'
    },
    {
    lastname: 'dog',
    food: 'treats',
    age: '22'
    }
]

// push into this empty object array
var values3 = [{}]

console.log(values3)
about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

The most generic approach which fulfills all of the OP's requirements should be based on Array.prototype.reduce. Its advantage comes with utilizing the additionally passed optional initial value as kind of configurable collector/accumulator object which will carry all the needed additional information / functions / result. Thus one can provide a reusable function with a customizable context/environment which one can adapt to ones needs.

var values1 = [{
  name: 'dog',
  surname: 'good',
  skills: 'programming',
}, {
  name: 'cat',
  surname: 'soft',
  skills: 'engineer',
}, {
  name: 'elephant',
  surname: 'big',
  skills: 'programming',
}];

var values2 = [{
  name: 'cat',
  food: 'fish',
  age: '12'
}, {
  name: 'elephant',
  food: 'leafs',
  age: '13'
}, {
  lastname: 'dog',
  food: 'treats',
  age: '22'
}];

function mergeItemsOfSameEntry(collector, item) {
  const {
    getSameEntryValue,
    getMergeSubType,
    comparisonItems,
    result
  } = collector;

  const itemValue = getSameEntryValue(item);
  const comparisonItem = comparisonItems
    .find(cItem => getSameEntryValue(cItem) === itemValue);

  if (comparisonItem !== null) {
    result.push({
      ...item,
      ...getMergeSubType(comparisonItem),
    });
  }
  return collector;
}

const values3 = values1.reduce(mergeItemsOfSameEntry, {

  getSameEntryValue: item => item.name ?? item.lastname,
  getMergeSubType: ({ age }) => ({ age }),
  comparisonItems: values2,
  result: [],

}).result;

console.log({ values3 });
.as-console-wrapper { min-height: 100%!important; top: 0; }

about 4 years ago · Santiago Gelvez Relatório

0

If you just want the key and value pair, you can do something like this:

var values1 = [
    {
    name: 'dog',
    surname: 'good',
    skills: 'programming'
    },
    {
    name: 'cat',
    surname: 'soft',
    skills: 'engineer'
    },
    {
    name: 'elephant',
    surname: 'big',
    skills: 'programming'
    }
]

var values2 = [
    {
    name: 'cat',
    food: 'fish',
    age: '12'
    },
    {
    name: 'elephant',
    food: 'leafs',
    age: '13'
    },
    {
    lastname: 'dog',
    food: 'treats',
    age: '22'
    }
]

// push into this empty object array
var values3 = [];

values1.forEach(eachValue1Obj => {
  const keys = Object.keys(eachValue1Obj);
  keys.forEach(eachKey => {
    values2.forEach(eachValue2Obj => {
      if (
        eachValue1Obj[eachKey] &&
        eachValue2Obj[eachKey] &&
        eachValue1Obj[eachKey] === eachValue2Obj[eachKey]
      ) {
        const x = {
          key: eachKey,
          value: eachValue1Obj[eachKey]
        };
        values3.push(x)
      }
    })
  })
})

console.log('Values 3 Array ==>', values3);

about 4 years ago · Santiago Gelvez 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