const first_name = document.getElementById("input_f_n").value;
const last_name = document.getElementById("input_l_n").value;
const email = document.getElementById("input_e").value;
const houseID = document.getElementById("input_houseID").value;
const userID = (users.filter(word => word.userID === index))[0].userID;
const fields = { userID, first_name, last_name, houseID, email };
const body = Object.keys(fields).reduce((acc, key) => {
if (fields[key] !== '' && fields[key] !== null && fields[key] !== undefined) {
acc[key] = fields[key];
}
return acc;
}, {});
const response = await fetch('http://127.0.0.1:8000/api/users/user/update', {
method: 'PUT',
headers: {'Content-Type': 'application/json', 'X-Requested-With': 'XMLHttpRequest',},
credentials: 'include',
body: JSON.stringify(body),
});
Или проверку можно организовать другим образом
//...
const body = { userID, first_name, last_name, houseID, email };
Object.keys(body).forEach(key => {
if (body[key] === '' || key[key] === null) {
delete body[key];
}
});
//...
Ну или совсем "в лоб":
//...
const body = {};
if (first_name !== '') {
body.first_name = first_name;
}
// Дальше проверки по всем значениям
// ...