I created a function that takes an array of objects and modifies one of the object's names to its corresponding property id. Here is the function implementation:
function prepareRidesData(rides, parks) {
if (!rides.length) return [];
const parksLookup = {};
parks.forEach(({ park_id, park_name }) => {
parksLookup[park_name] = park_id;
});
const updatedRides = rides.map((ride) => {
ride.park_id = parksLookup[ride.park_name];
delete ride.park_name;
console.log(ride);
return ride;
})
return updatedRides;
}
I ran some tests using this format:
test("Return two park name with id only", () => {
const input =([{
ride_name: "Tidal Wave",
year_opened: 2000,
park_name: "Thorpe Park",
votes: 1,
},
{
ride_name: 'Nemesis',
year_opened: 1994,
park_name: 'Alton Towers',
votes: 5,
}]);
const actual = prepareRidesData(input);
expect(actual).toEqual([ {
ride_name: "Tidal Wave",
year_opened: 2000,
park_id: 1,
votes: 1,
},
{
ride_name: "Nemesis",
year_opened: 1994,
park_id: 5,
votes: 1,
}]);
});
I encountered an error - TypeError: Cannot read properties of undefined (reading 'forEach')
I need assistance in identifying where I made an error that is causing it not to execute correctly.
The goal is to replace the park name with its corresponding park id.