const todos = { todo1: { isCompleted: true }, todo2: { isCompleted: true }, todo3: { isCompleted: true }, }; // Check if the Object is not empty if (Object.keys(todos).length !== 0) { const allAreEqual = (todos) => { const res = []; // Create a new empty array // Loop through all the objects for (const key in todos) { const todo = todos[key]; // Make sure the nested objects are objects and not strings or other... if (todo && typeof todo === 'object') { // Check if the property we're looking for exists in the nested object if (Object.prototype.hasOwnProperty.call(todo, 'isCompleted')) { // Add all the values from the nested objects in the array res.push(todo.isCompleted); } } } // .every returns a boolean if the condition is true // res array looks like this e.g.: ['true','true','true','true','false'] // ['true','true','true','false'] = false // ['true','true','true','true'] = true return res.every((v) => v === true); }; //return the result return allAreEqual(todos); }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter