Viết hàm containsAll() nhận object và mảng chuỗi, trả true nếu object có đủ các key trong mảng.
Duyệt mảng, kiểm tra key tồn tại trong object, thiếu một thì trả false.
Ví dụ
const obj = { 'name': 'Ashish Kumar','dob': '12/07/1991','gen': 'M','isEmployed': true,'jobType': 'full-time' }; const obj2 = { 'name': 'Ashish Kumar','dob': '12/07/1991','gen': 'M','jobType': 'full-time' }; const arr = ['dob', 'name', 'gen', 'isEmployed', 'jobType']; const containsAll = (obj, arr) => { for(const str of arr){ if(Object.keys(obj).includes(str)){ continue; }else{ return false; } } return true; }; console.log(containsAll(obj, arr)); console.log(containsAll(obj2, arr));Kết quả
Đầu ra console:
true false