In my ES6 coding endeavors, I am striving for a specific outcome. Imagine I have an array of objects titled schools
.
let schools = [
{name: 'YorkTown', country: 'Spain'},
{name: 'Stanford', country: 'USA'},
{name: 'Gymnasium Achern', country: 'Germany'}
];
My goal is to create a function called editSchoolName
that will accept 3 parameters: schools
(the predefined array), oldName
, and name
.
The oldName
parameter will contain the current name of the school that needs to be updated with the value passed in the name
parameter.
To maintain the original state of the schools
variable, I plan to utilize a map
function that will generate a new array with the desired modifications.
The editSchoolName
function will be invoked as follows -
var updatedSchools = editSchoolName(schools, "YorkTown", "New Gen");
In this case, the name YorkTown
should be substituted with New Gen
. Therefore, the resulting array updatedSchools
should appear as -
let updatedSchools = [
{name: 'New Gen', country: 'Spain'},
{name: 'Stanford', country: 'USA'},
{name: 'Gymnasium Achern', country: 'Germany'}
];
This is how the editSchoolName
function is structured -
const editSchoolName = (schools, oldName, name) =>
schools.map(item => {
if (item.name === oldName) {
/* This is the section where the logic needs to be applied */
} else {
return item;
}
});
I need assistance in modifying the editSchoolName
function to achieve the desired outcome as described above.