JS 对象的key的值,丢到数组的每一项中

 const data = {  
      userName: '崔文斌',  
      userPhone: '1888888888',  
      userEducation: '本科'  
    };  
      
    const fields = [  
      { id: 1, name: '用户名', value: '',type: 'userName' },  
      { id: 2, name: '手机号', value: '' ,type: 'userPhone'},  
      { id: 3, name: '学历', value: '' ,type: 'userEducation'}  
    ];  
      
    // 根据字段名检查并将值插入到对应的对象中  
    for (const field of fields) {  
      console.log(field);
      if (field.type in data) { 
        console.log(field, 'field');
        field.value = data[field.type];  
        // break;  // 找到匹配的字段后跳出循环  
      }  
    }  
    console.log(fields)

这个in 不知道啥意思 可以看我这篇文章判读对象是否有这个key

猜你喜欢

转载自blog.csdn.net/cwb_2120/article/details/132524393