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

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

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 Denunciar

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 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