Return object with default values from array in Javascript

后端 未结 4 454
离开以前
离开以前 2021-01-22 14:10
    const fields = [\'email\', \'password\'];

    const objFields = {};
    fields.forEach(value => {
      objFields[value] = \'\';
    });

    console.log(objFiel         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-22 14:30

    Whenever you're looking for reducing an array of values to one value, you're looking for .reduce()

    state = {
      fields: fields.reduce((acc, key) => ({...acc, [key]: ''}), {}),
    };
    

提交回复
热议问题