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

116
Views
Extract data from array if IDs match

I am struggling to find out a solution to my problem, but at the moment I cannot come up with the right one.

When I have my two arrays of objects I want to filter based on category IDs and extract the data from the second one into a new array for example :

    const array1 = [
         {id: 1, name: 'Tropical'},
         {id: 2, name: 'Common'}
]
    
    const array2 = [
         {id:1, name: 'Banana', category_id: 1},
         {id:2, name: 'Mango', category_id: 1},
         {id:3, name: 'Apple', category_id: 2},
    ]

And when click happens I detect the first ID and render the new array only with data that matches the ID. Click Tropical New array :

[
 {id:1, name: 'Banana', category_id: 1},
 {id:2, name: 'Mango', category_id: 1},
]

I would be happy if someone give me a hint on how can I tackle this problem. Thanks !

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

0

Use reduce to map over each item in array1 and filter to grab the items of that category_id

const array1 = [{
    id: 1,
    name: 'Tropical'
  },
  {
    id: 2,
    name: 'Common'
  }
]

const array2 = [{
    id: 1,
    name: 'Banana',
    category_id: 1
  },
  {
    id: 2,
    name: 'Mango',
    category_id: 1
  },
  {
    id: 3,
    name: 'Apple',
    category_id: 2
  },
]

const obj = array1.reduce((acc, cur) => {
  acc[cur.name] = array2.filter(v => v.category_id === cur.id)
  return acc
}, {})

console.log(obj)

about 4 years ago · Juan Pablo Isaza Report

0

You could do something like filtering array2 and taking all the elements that have Tropical as name in array1.

const array1 = [
     {id: 1, name: 'Tropical'},
     {id: 2, name: 'Common'}
]

const array2 = [
     {id:1, name: 'Banana', category_id: 1},
     {id:2, name: 'Mango', category_id: 1},
     {id:3, name: 'Apple', category_id: 2},
]

// take tropical friuts
let tropicalFriuts = array2.filter(x => x.category_id === array1.filter(y => y.name === 'Tropical')[0].id);
console.log(tropicalFriuts);

about 4 years ago · Juan Pablo Isaza Report

0

If I understood your problem you want before find the id, based on the name of the category, and later filter array2 data based on this id.

const array1 = [{
    id: 1,
    name: 'Tropical'
  },
  {
    id: 2,
    name: 'Common'
  }
]

const array2 = [{
    id: 1,
    name: 'Banana',
    category_id: 1
  },
  {
    id: 2,
    name: 'Mango',
    category_id: 1
  },
  {
    id: 3,
    name: 'Apple',
    category_id: 2
  },
]

const id_key = array1.find(item=>item.name === 'Tropical').id;
const result = array2.filter(item=>item.category_id === id_key);
console.log(result);

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!