Update the attribute value of an object using the map function in ES6

前端 未结 6 2151
孤城傲影
孤城傲影 2021-02-13 11:09

I am trying to code this in ES6. Below is what I am trying to achieve. Let\'s say I have an array of objects called schools.

let schools = [
    {na         


        
6条回答
  •  旧时难觅i
    2021-02-13 11:54

       const editSchoolName = (schools, oldName, newName) =>
        schools.map(({name, ...school }) => ({ ...school, name: oldName === name ? newName : name }));
    

    You could shorten it by using a ternary.

提交回复
热议问题