It is necessary to compare the array with the required fields with the object keys, if true, otherwise false.
const requiredFields = [ 'title', 'price', 'discount' ]; let form1 = { title: 'Товар Телепорт бытовой VZHIH-101', price: 7800, discount: 0 }; let form2 = { title: 'Товар Телепорт бытовой VZHIH-101', discount: 10 } if ( isValidPosition(form1, requiredFields) ) { console.log('Форма №1 заполнена верно'); } else { console.log('В форме №1 не заполнены необходимые поля'); } if ( isValidPosition(form2, requiredFields) ) { console.log('Форма №2 заполнена верно'); } else { console.log('В форме №2 не заполнены необходимые поля'); } function isValidPosition(form, settings) { let formKeys = Object.keys(form); console.log(formKeys); for (let i = 0; i < settings.length; i++) { if (settings.indexOf(formKeys[i]) === -1) { console.log('Форма заполнена не верно'); return false; } else { console.log('Форма заполнена верно'); return true; } } }
return true;remove from the loop, else can be removed. - greybutton