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

前端 未结 6 2128
孤城傲影
孤城傲影 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条回答
  • 2021-02-13 11:30

    If you want to edit only the commented part:

    const editSchoolName = (schools, oldName, name) =>
        schools.map(item => {
            if (item.name === oldName) {
              var newItem = Object.assign({},item);
              newItem.name = name;
              return newItem;
            }
            else{
              return item;
            }
        });
    
    0 讨论(0)
  • 2021-02-13 11:43

    let schools = [{
        name: 'YorkTown',
        country: 'Spain'
      },
      {
        name: 'Stanford',
        country: 'USA'
      },
      {
        name: 'Gymnasium Achern',
        country: 'Germany'
      }
    ];
    
    let updatedSchools = [{
        name: 'New Gen',
        country: 'Spain'
      },
      {
        name: 'Stanford',
        country: 'USA'
      },
      {
        name: 'Gymnasium Achern',
        country: 'Germany'
      }
    ];
    
    const editSchoolName = ((schools, oldName, name) =>{
      schools.map(item => {
        if (item.name === oldName) {
          item.name = name;
          return item.name;
        } else {
          return item;
        }
      });
      console.log(schools);
    });
    
    editSchoolName(schools, 'YorkTown', "New Gen");

    0 讨论(0)
  • 2021-02-13 11:45

    I wonder how come none of the answers give simple solution

    const editSchoolName = (schools, oldName, newName) =>
          schools.map(school => { if (school.name === oldName) school.name = newName;
          return school; 
    });
    
    0 讨论(0)
  • 2021-02-13 11:48

    try this, ES6 Object.assign() to create copy of array element and update new object.

    let schools = [{
            name: 'YorkTown',
            country: 'Spain'
        },
        {
            name: 'Stanford',
            country: 'USA'
        },
        {
            name: 'Gymnasium Achern',
            country: 'Germany'
        }
    ];
    
    const editSchoolName = (schools, oldName, name) => {
        return schools.map(item => {
            var temp = Object.assign({}, item);
            if (temp.name === oldName) {
                temp.name = name;
            }
            return temp;
        });
    }
    
    var updatedSchools = editSchoolName(schools, "YorkTown", "New Gen");
    console.log(updatedSchools);
    console.log(schools);

    0 讨论(0)
  • 2021-02-13 11:48

    You need to return the updated object:

    const editSchoolName = (schools, oldName, name) =>
      schools.map(item => {
          if (item.name === oldName) {
            return {...item, name};
          } else {
            return item;
          }
    });
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题