Return object with default values from array in Javascript

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

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

    console.log(objFiel         


        
4条回答
  •  孤街浪徒
    2021-01-22 14:42

    You could map objects and assign all to a single object.

    const
        fields = ['email', 'password'],
        object = Object.assign({}, ...fields.map(key => ({ [key]: '' })));
    
    console.log(object);

提交回复
热议问题