OOP - Compare two object arrays and create new array containing only those objects with matching ids

See link above for the challenge in question.

I need to fill an empty array with the objects listed in a second array containing the same ids as those found in a third object array.

Any help appreciated.

EDIT: I am also posting the challenge below since I have had experienced problems sharing links from repl.it in the past.

Click for code.
let markers = [];


let filteredEvents = [
      {
        id: 101,
        title: 'All Day Event',
        start: '2017-11-01'
      },
      {
        id: 301,
        title: 'Lunch',
        start: '2017-11-07T14:00:00'
      },
      {
        id: 102,
        title: 'Meeting',
        start: '2017-11-09T16:00:00'
      }];
      

let locations = [
    {
      id: 101,
      name: 'Apple',
      lat: 41.3879528,
      lng: 2.1662727
    },
    {
      id: 102,
      name: 'Phone House',
      lat: 41.3864665,
      lng: 2.1706846
    },
    {
      id: 103,
      name: 'El Corte Inglés',
      lat: 41.3802726,
      lng: 2.1410884
    },
    {
      id: 201,
      name: 'Subway',
      lat: 41.385913,
      lng: 2.1655988
    },
    {
      id: 202,
      name: 'La Flauta',
      lat: 41.3864213,
      lng: 2.1612996
    },
    {
      id: 203,
      name: 'Renoir Floriblanca',
      lat: 41.3830584,
      lng: 2.1600052
    },
    {
      id: 301,
      name: 'La Malandrina',
      lat: 41.4008735,
      lng: 2.2031469
    },
    {
      id: 302,
      name: "Domino's Pizza",
      lat: 41.3864213,
      lng: 2.1612996
    },
    {
      id: 303,
      name: 'Madre Lievito',
      lat: 41.3967925,
      lng: 2.2016712
    }
  ];

Let me get this straight - you want to fill the markers array with location objects that match the id of a filtered Event? Can you provide an example of what you want the output to be, like this?

markers = [
   {
      id: 101,
      name: 'Apple',
      lat: 41.3879528,
      lng: 2.1662727
    },
    {
      id: 301,
      name: 'La Malandrina',
      lat: 41.4008735,
      lng: 2.2031469
    },
    {
      id: 102,
      name: 'Phone House',
      lat: 41.3864665,
      lng: 2.1706846
    },
]

Yes, that’s the outcome I am looking for.

Apologies for the lack of clarity.

This seems a lot like something you would be doing in a mongo database. Anyway, for your example, I would probably use a for...of loop for every location, and then compare that to the ids. It might be easier if you pulled off the ids first…

let ids = []

// put each event id in ids if it does not exist there already
for (event of events) {
  if(!ids.includes(event.id)) ids.push(event.id)
}

// place each location in markers if it matches a correct id
for (location of locations) {
  if (ids.includes(location.id)) markers.push(location)
}

This is just some pseudocode that I can think of that might work off the top of my head. @camperextraordinaire can probably find a better solution for you if he’s interested. It is a fun algorithm to solve either way.

Thanks Isaac. I’ve worked it out.

Click for my solution
function print(){
  filteredEvents.map(function(e) {
    locations.find(function(l){
      if (e.id == l.id){markers.push(l)}
    });
  }); 
}
1 Like