>  기사  >  웹 프론트엔드  >  [Leetcode] 객체가 비어 있습니까?

[Leetcode] 객체가 비어 있습니까?

Linda Hamilton
Linda Hamilton원래의
2024-10-02 22:31:30659검색

[Leetcode] Is Object Empty

Question Link: https://leetcode.com/problems/is-object-empty/description/

[Problem Statment]

Given an object or an array, return if it is empty.

  • An empty object contains no key-value pairs.
  • An empty array contains no elements.

You may assume the object or array is the output of JSON.parse.

[Understanding Question]

  1. Sooo... we’re getting either an Array or an Object as the parameter to this function called obj. (Yeah, I know, super creative name, but it'll make sense later!)

  2. Our task? Perform some magic coding tricks to first figure out if this obj is an array or an object. Then, based on that knowledge, we’ll decide what to do next.

  3. Finally, we need to check if this object/array is empty or not. Simple, right? (Or is it...)

[Key Points]

  1. First things first: "An array is technically an object in JavaScript." Yep! That’s right—arrays are a special type of object in JS. So, don't get confused!

  2. Object Detection 101: We can check if the parameter is actually an object (or array) by using typeof obj === 'object' && obj !== null. Why the !== null part? Well, because null is a special case in JS. Even though it's technically of type object, it's like the empty void of nothingness. So, we exclude that, just in case (even though the problem didn’t explicitly mention it, it’s a good habit).

  3. Undefined and null Elements: Both objects and arrays can have properties/elements that are undefined or null. But no worries—they'll still count when we check the length. They don’t get a free pass from our validation!

  4. Is it an Array?: We can check if the parameter is an array using Array.isArray(obj). No need to check again if it’s an object, though—we already did that earlier.

[Solution Code]

var isEmpty = function (obj) {
  if (typeof obj === "object" && obj !== null) { // Check if it's an object and not null
    if (Array.isArray(obj)) { // If it's an array
      return obj.length === 0; // Return true if it's empty
    } else { // If it's an object
      return Object.keys(obj).length === 0; // Return true if there are no keys
    }
  }
  return false; // If it's neither an object nor an array, return false
};

[Breaking It Down... even further]

Step 1: Check if it’s an object
First, we use typeof obj === 'object' && obj !== null to ensure the input is an object and not null. (Because we all know that typeof null === 'object is one of JavaScript’s weird quirks. Thanks, JavaScript!)

Step 2: Is it an array?
We use Array.isArray(obj) to distinguish between arrays and regular objects. Arrays have a length property, so we can directly check if obj.length === 0.

Step 3: Is it an object?
If it’s not an array, we check if it's an object using Object.keys(obj).length === 0. This method returns the number of keys in the object. No keys? It's empty!

Step 4: Default case
If the input isn’t an object (or it’s null), we return false because the question specifies we're working with either objects or arrays only.

위 내용은 [Leetcode] 객체가 비어 있습니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.