0

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 !

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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)

1 Comment

Thanks for the solution i will definitely check it out .
1

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

Comments

1

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

1 Comment

Yes, that's about right. I had almost the same answer but it was not quite right. Thanks for the snippet. I understand it now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.