
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Check if object contains all keys in JavaScript array
We are required to write a function containsAll() that takes in two arguments, first an object and second an array of strings. It returns a boolean based on the fact whether or not the object contains all the properties that are mentioned as strings in the array.
So, let’s write the code for this. We will iterate over the array, checking for the existence of each element in the object, if we found a string that’s not a key of object, we exit and return false, otherwise we return true.
Here is the code for doing the same −
Example
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));
Output
The output of the above code in the console will be −
true false
- Related Articles
- JavaScript: replacing object keys with an array
- How to check if an array contains integer values in JavaScript ?
- Check if an array contains all elements of a given range in Python
- Method to check if array element contains a false value in JavaScript?
- Sort object array based on another array of keys - JavaScript
- Check if the given array contains all the divisors of some integer in Python
- Check if list contains all unique elements in Python
- Make strings in array become keys in object in a new array in JavaScript?
- How do we check if an object is an array in Javascript?
- How do I check if an array includes an object in JavaScript?
- Check if a SortedList object contains a specific value in C#
- Check if an array object is equal to another array object in C#
- How Check if object value exists not add a new object to array using JavaScript ?
- Compare keys & values in a JSON object when one object has extra keys in JavaScript
- Fetching object keys using recursion in JavaScript

Advertisements