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

83
Views
map unequal length of array of object

I have two arrays of object data that is the unequal length I just want to create a third array that is given below

first input array data

let firstArray = [{
    id: '0',
    name: 'shanu',
    age: '21'
  },
  {
    id: '1',
    name: 'bhanu',
    age: '21'
  },
  {
    id: '2',
    name: 'chary',
    age: '21'
  },
]

second set of array of data

let secondArray = [{
    id: '10',
    name: 'shanu',
    age: '28'
  },
  {
    id: '11',
    name: 'raanu',
    age: '29'
  },
  {
    id: '12',
    name: 'chary',
    age: '30'
  },
  {
    id: '15',
    name: 'kushal',
    age: '31'
  },
]

output array- if second set name watch with first set of data replace the second set id with first set it

let outputArray = [{
    id: '0',
    name: 'shanu',
    age: '28'
  },
  {
    id: '11',
    name: 'raanu',
    age: '29'
  },
  {
    id: '2',
    name: 'chary',
    age: '30'
  },
  {
    id: '15',
    name: 'kushal',
    age: '31'
  },
]

I'm not able to figure out how to map and filter together two different length of carry of array of object

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could map the secondArray and replace the id if name is found in firstArray:

let firstArray = [
  { id: '0', name: 'shanu', age: '21' },
  { id: '1', name: 'bhanu', age: '21' },
  { id: '2', name: 'chary', age: '21' }
]

let secondArray = [
  { id: '10', name: 'shanu', age: '28' },
  { id: '11', name: 'raanu', age: '29' },
  { id: '12', name: 'chary', age: '30' },
  { id: '15', name: 'kushal', age: '31' }
]

let thirdArray = secondArray.map((item) => {
  let found = firstArray.find((i) => i.name === item.name)
  return { ...item, id: found ? found.id : item.id }
})

console.log(thirdArray)

about 4 years ago · Juan Pablo Isaza Report

0

The below may be one method to achieve the desired objective:

Code Snippet

const updateId = (arr1, arr2) => ( // arr1 has the up-to-date / current "id"
  arr2.map(({name, ...rest}) => (
    arr1.some(ob => ob.name === name)
    ? {name, ...rest, id: arr1.find(ob => ob.name === name)?.id}
    : {name, ...rest}
  ))
);

const firstArray = [{
    id: '0',
    name: 'shanu',
    age: '21'
  },
  {
    id: '1',
    name: 'bhanu',
    age: '21'
  },
  {
    id: '2',
    name: 'chary',
    age: '21'
  },
]

const secondArray = [{
    id: '10',
    name: 'shanu',
    age: '28'
  },
  {
    id: '11',
    name: 'raanu',
    age: '29'
  },
  {
    id: '12',
    name: 'chary',
    age: '30'
  },
  {
    id: '15',
    name: 'kushal',
    age: '31'
  },
];

console.log(updateId(firstArray, secondArray));

Explanation

  • iterate over arr2 (ie, 'secondArray')
  • for name, check if arr1 (ie, 'firstArray) has a match
  • if yes, update the id by using .find
  • else, keep the info in arr2 iteration as-is
about 4 years ago · Juan Pablo Isaza Report

0

How about this:

Loop through the 2nd array, look for name matches in the 1st array, If a match exists, update the 2nd array.

for (i=0;i<secondArray.length;i++){
  for(j=0; j<firstArray.length; j++){
    if(secondArray[i].name == firstArray[j].name){
      secondArray[i].id = firstArray[j].id
    } 
  }
  outputArray[i] = secondArray[i]
}

The output array would be the same as your 2nd array. If you don't want to change anything in your 2nd array, you could copy the array first and then make the changes in the double for-loop on the output array instead of the second array.

about 4 years ago · Juan Pablo Isaza 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!