-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2727-IsObjectEmpty.js
More file actions
43 lines (35 loc) · 1.21 KB
/
2727-IsObjectEmpty.js
File metadata and controls
43 lines (35 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// 2727. Is Object Empty
// 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.
// 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 <= 10^5
/**
* @param {Object|Array} obj
* @return {boolean}
*/
var isEmpty = function(obj) {
// // 如果是数组判断,数组长度
// if (Array.isArray(obj)) return obj.length === 0;
// // 如果是对象,判读对象的 keys 数组的长度
// return Object.keys(obj).length === 0;
// 判断是否对象是否能循环
for (let key in obj) return false;
return true;
// 判断是否是 [] 数组 {} 对象
// return JSON.stringify(obj).length<=2?true:false;
};