The value of the key of the JS object is thrown into each item of the array.

 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)

I don’t know what this in means. You can read my article to determine whether the object has this key.

Guess you like

Origin blog.csdn.net/cwb_2120/article/details/132524393