Is Object Empty

Updated on 03 June, 2025
Is Object Empty header image

Problem Statement

The task is to determine whether a given object or array, which results from a JSON.parse action, is empty or not. The characteristics of an empty structure are defined as follows:

  • An empty object ({}) contains no key-value pairs.
  • An empty array ([]) holds no elements.

The object or array to be tested is assured to be a valid output of JSON.parse, meaning it's well-formed and correctly structured according to JSON standards.

Examples

Example 1

Input:

obj = {"x": 5, "y": 42}

Output:

false

Explanation:

The object has 2 key-value pairs so it is not empty.

Example 2

Input:

obj = {}

Output:

true

Explanation:

The object doesn't have any key-value pairs so it is empty.

Example 3

Input:

obj = [null, false, 0]

Output:

false

Explanation:

The array has 3 elements so it is not empty.

Constraints

  • obj is a valid JSON object or array
  • 2 <= JSON.stringify(obj).length <= 105

Approach and Intuition

The approach to solve this problem is straightforward given the simplicity of the conditions for emptiness:

  1. Check the type of the given entity (whether it's an object or an array).
    • For an object, ascertain its emptiness by checking if it has any own enumerable properties. In JavaScript, this can be done using Object.keys(obj).length === 0.
    • For an array, check if its length is zero, e.g., array.length === 0.

The examples provided further clarify the behavior:

  1. Example 1: An object with properties {x: 5, y: 42}

    • This is not empty as it contains two key-value pairs. The output is false.
  2. Example 2: An empty object {}

    • It contains no key-value pairs, hence it is empty. The output is true.
  3. Example 3: An array of elements [null, false, 0]

    • Despite the elements being falsy values, the array contains three items. Therefore, it is not empty and the output is false.

Any implementation following these steps, respecting the described logic and constraints, should effectively determine if the input object or array is empty. The constraints assure the object's or array's minimum and maximum possible sizes, ensuring the solution remains efficient within these bounds.

Solutions

  • JavaScript
js
var checkEmpty = function(data) {
    for (const key in data) return false;
    return true;
};

The provided JavaScript function checkEmpty is designed to determine whether an object is empty. Here’s how it operates:

  • It accepts one parameter, data, which is expected to be an object.
  • The function utilizes a for-in loop to iterate over properties of the object.
  • If the object has at least one property, the loop executes and the function immediately returns false, indicating that the object is not empty.
  • If the loop completes without finding any properties, it means the object is empty, and thus the function returns true.

Use this function to quickly verify if an object in your JavaScript code has no properties. Simply pass the object as an argument to checkEmpty, and it will evaluate the emptiness of the object.

Comments

No comments yet.