Home > Mobile >  Extract data from array if IDs match
Extract data from array if IDs match

Time:02-19

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 !

CodePudding user response:

Correct me if I am wrong, So you need a function that received a categoryId and you need to filter out array2 based on that category_id

You can try this

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
  },
]

function categoryFruits(categoryId) {
  return array2.filter(obj => obj.id === categoryId)
}

console.log(categoryFruits(3));

CodePudding user response:

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)

CodePudding user response:

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);

CodePudding user response:

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);

  • Related