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

313
Views
How do I compare and combine two object array into one?

I have been stuck trying to implement this forever. do you think you could help me out merging two array objects based on the key PartitionKey and if the PartitionKey don't match up, still add it to the array anyway?

let arr1 =[
  { PartitionKey: '047', AttributionCount: 61 },
  { PartitionKey: '043', AttributionCount: 136 },
  { PartitionKey: '053', AttributionCount: 36 }
];

let arr2 = [
  { PartitionKey: '043', ReportCount: 5 },
  { PartitionKey: '047', ReportCount: 2 },
  { PartitionKey: '045', ReportCount: 2 },
  { PartitionKey: '041', ReportCount: 3 }
]

let arr3 = arr1.map((item, i) => Object.assign({}, item, arr2[i]));

console.log(arr3);

I would like to return the result of

[
  { PartitionKey: '047', ReportCount: 2, AttributionCount: 61 },
  { PartitionKey: '043', ReportCount: 5, AttributionCount: 136 },
  { PartitionKey: '053', ReportCount: 0, AttributionCount: 36 },
  { PartitionKey: '045', ReportCount: 2, AttributionCount: 0},
  { PartitionKey: '041', ReportCount: 3, AttributionCount: 0}
]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can do it like this:

let arr1 =[
  { PartitionKey: '047', AttributionCount: 61 },
  { PartitionKey: '043', AttributionCount: 136 },
  { PartitionKey: '053', AttributionCount: 36 }
];

let arr2 = [
  { PartitionKey: '043', ReportCount: 5 },
  { PartitionKey: '047', ReportCount: 2 },
  { PartitionKey: '045', ReportCount: 2 },
  { PartitionKey: '041', ReportCount: 3 }
]

let arr3 = arr2.map((item, i) => {
  const itemArray1 = arr1.find(x => x.PartitionKey == item.PartitionKey) || {AttributionCount: 0}
  return {...item, ...itemArray1}
});

console.log('arr3', arr3);

about 4 years ago · Juan Pablo Isaza Report

0

Try using object.assign() method for this problem.

e.g: const returnedTarget = Object.assign(target, source);

reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

about 4 years ago · Juan Pablo Isaza Report

0

my way...

let arr1 = 
  [ { PartitionKey: '047', AttributionCount:  61 } 
  , { PartitionKey: '043', AttributionCount: 136 } 
  , { PartitionKey: '053', AttributionCount:  36 } 
  ] 
let arr2 = 
  [ { PartitionKey: '043', ReportCount: 5 } 
  , { PartitionKey: '047', ReportCount: 2 } 
  , { PartitionKey: '045', ReportCount: 2 } 
  , { PartitionKey: '041', ReportCount: 3 } 
  ] 

const result = 
  Object.entries( 
    arr1
    .concat(arr2)
    .reduce((res,{PartitionKey,...info}) =>
      {
      if (!res[PartitionKey]) res[PartitionKey] = info
      else      Object.assign(res[PartitionKey], info )
      return res
      }
      ,{})
  ).map( ([k,v]) =>
    ({PartitionKey:k, AttributionCount:0, ReportCount:0, ...v}))

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

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!