2

Array One:

Cuisines in Profile      =======>>  [{"id": 2, "name": "Indian"}, {"id": 4, "name": "Mexican"}, {"id": 5, "name": "Mediterranean"}, {"id": 6, "name": "Middle Eastern"}, {"id": 7, "name": "Chinese"}, {"id": 8, "name": "Japanese"}, {"id": 9, "name": "Italian"}, {"id": 10, "name": "Pick for Me"}, {"id": 16, "name": "BBQ"}]

Array Two:

Selected Cuisines        =======>>  [{"cuisine_id": 6, "id": 1260}, {"cuisine_id": 16, "id": 1262}, {"cuisine_id": 8, "id": 1268}, {"cuisine_id": 10, "id": 1269}]

Now I want to get a new filtered objects from Array one where id from Array one and cuisine_id from Array two are the same

Expected Output:

[{"id": 6, "name": "Middle Eastern"}, {"id": 8, "name": "Japanese"}, {"id": 10, "name": "Pick for Me"}, {"id": 16, "name": "BBQ"}]
Muhammad Umar
  • 190
  • 1
  • 3
  • 15

2 Answers2

3

const cuisines = [
  { id: 2, name: 'Indian' },
  { id: 4, name: 'Mexican' },
  { id: 5, name: 'Mediterranean' },
  { id: 6, name: 'Middle Eastern' },
  { id: 7, name: 'Chinese' },
  { id: 8, name: 'Japanese' },
  { id: 9, name: 'Italian' },
  { id: 10, name: 'Pick for Me' },
  { id: 16, name: 'BBQ' },
];
const selected = [
  { cuisine_id: 6, id: 1260 },
  { cuisine_id: 16, id: 1262 },
  { cuisine_id: 8, id: 1268 },
  { cuisine_id: 10, id: 1269 },
];
const result = cuisines.filter(c => selected.some(s => s.cuisine_id === c.id));
console.log(result);

An alternate way to achieve the same result would be to use a for loop.

const cuisines = [
  { id: 2, name: 'Indian' },
  { id: 4, name: 'Mexican' },
  { id: 5, name: 'Mediterranean' },
  { id: 6, name: 'Middle Eastern' },
  { id: 7, name: 'Chinese' },
  { id: 8, name: 'Japanese' },
  { id: 9, name: 'Italian' },
  { id: 10, name: 'Pick for Me' },
  { id: 16, name: 'BBQ' },
];

const selected = [
  { cuisine_id: 6, id: 1260 },
  { cuisine_id: 16, id: 1262 },
  { cuisine_id: 8, id: 1268 },
  { cuisine_id: 10, id: 1269 },
];

const result = [];

for (const cuisine of cuisines) {
  for (const s of selected) {
    if (s.cuisine_id === cuisine.id) {
      result.push(cuisine);
      break;
    }
  }
}

console.log(result);

In terms of performance, the filter and some method approach is likely to be faster than the for loop, especially for large arrays. This is because the filter and some methods are optimised for performance and can take advantage of multiple CPU cores, whereas the for loop approach is limited to a single thread.

Parvesh Kumar
  • 1,104
  • 7
  • 16
1

Method 1: Using filter() and find() functions-

From the cuisines array filter those items which get found in the selected array.

Here is the demo-

const cuisines = [
  { id: 2, name: 'Indian' },
  { id: 4, name: 'Mexican' },
  { id: 5, name: 'Mediterranean' },
  { id: 6, name: 'Middle Eastern' },
  { id: 7, name: 'Chinese' },
  { id: 8, name: 'Japanese' },
  { id: 9, name: 'Italian' },
  { id: 10, name: 'Pick for Me' },
  { id: 16, name: 'BBQ' },
];
const selected = [
  { cuisine_id: 6, id: 1260 },
  { cuisine_id: 16, id: 1262 },
  { cuisine_id: 8, id: 1268 },
  { cuisine_id: 10, id: 1269 },
];
const result = cuisines.filter(c => selected.find(s => s.cuisine_id == c.id))
console.log(result);

Method 2: Using filter(), map() and includes() functions-

Apply map() on the selected array to get only cuisine_id in the array and then use includes to check if this mapped array includes the requested cuisine id.

Here is the demo-

const cuisines = [
  { id: 2, name: 'Indian' },
  { id: 4, name: 'Mexican' },
  { id: 5, name: 'Mediterranean' },
  { id: 6, name: 'Middle Eastern' },
  { id: 7, name: 'Chinese' },
  { id: 8, name: 'Japanese' },
  { id: 9, name: 'Italian' },
  { id: 10, name: 'Pick for Me' },
  { id: 16, name: 'BBQ' },
];
const selected = [
  { cuisine_id: 6, id: 1260 },
  { cuisine_id: 16, id: 1262 },
  { cuisine_id: 8, id: 1268 },
  { cuisine_id: 10, id: 1269 },
];
const result = cuisines.filter(c => selected.map(s => s.cuisine_id).includes(c.id))
console.log(result);
Neha Soni
  • 3,935
  • 2
  • 10
  • 32