Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!