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

81
Views
Create a new array from two different array after matching condition

I have two array, want to create a new array that match array1.id == array2.toyId.

Array1 = [
  {
    id: 2,
    name: 'arujn'
  },
  {
    id: 3,
    name: 'abhinav'
  }
]

Array2 = [
  {
    id: 1,
    toyId: 2,
    label: 'label456',
    type: 'text56'
  },
  {
    id: 2,
    toyId: 2,
    label: 'label22',
    type: 'text88'
  },
  {
    id: 3,
    toyId: 3,
    label: 'labe ffum',
    type: 'text35'
  },
  {
    id: 4,
    toyId: 3,
    label: 'label 8',
    type: 'text66'
  }
]

So, new array have name from array1 and label & type from array2,

That array will look like this:-

I am using javascript for this.

Array3 = [
  {
    name: 'arjun',
    newdata: [
      {
        label: 'label456',
        type: 'text56'
      },
      {
       label: 'label22',
       type: 'text88'
      }
    ]
  },
  {
    name: 'abhinav',
    newdata: [
      {
        label: 'labe ffum',
        type: 'text35'
      },
      {
       label: 'label 8',
       type: 'text66'
      }
    ]
  }
]

Please help me to solve this, Thank you in advance.

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

0

const Array1 = [
  {id: 2,name: 'arujn'},
  {id: 3,name: 'abhinav'}],
 Array2 = [
  {id: 1,toyId: 2,label: 'label456',type: 'text56'},
  {id: 2,toyId: 2,label: 'label22',type: 'text88'},
  {id: 3,toyId: 3,label: 'labe ffum',type: 'text35'},
  {id: 4,toyId: 3,label: 'label 8',type: 'text66'}];

const Array3 = Array1.map(element => ({name: element.name, newdata: Array2.filter(obj => obj.toyId === element.id).map(({label, type}) => ({label, type}) )}))

console.log(Array3);

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!