{ "data": { "question": { "questionId": "2804", "questionFrontendId": "2705", "categoryTitle": "JavaScript", "boundTopicId": 2289205, "title": "Compact Object", "titleSlug": "compact-object", "content": "
Given an object or array obj
, return a compact object. A compact object is the same as the original object, except with keys containing falsy values removed. This operation applies to the object and any nested objects. Arrays are considered objects where the indices are keys. A value is considered falsy when Boolean(value)
returns false
.
You may assume the obj
is the output of JSON.parse
. In other words, it is valid JSON.
\n
Example 1:
\n\n\nInput: obj = [null, 0, false, 1]\nOutput: [1]\nExplanation: All falsy values have been removed from the array.\n\n\n
Example 2:
\n\n\nInput: obj = {"a": null, "b": [false, 1]}\nOutput: {"b": [1]}\nExplanation: obj["a"] and obj["b"][0] had falsy values and were removed.\n\n
Example 3:
\n\n\nInput: obj = [null, 0, 5, [0], [false, 16]]\nOutput: [5, [], [16]]\nExplanation: obj[0], obj[1], obj[3][0], and obj[4][0] were falsy and removed.\n\n\n
\n
Constraints:
\n\nobj is a valid JSON object
2 <= JSON.stringify(obj).length <= 106
现给定一个对象或数组 obj
,返回一个 精简对象 。精简对象 与原始对象相同,只是将包含 假 值的键移除。该操作适用于对象及其嵌套对象。数组被视为索引作为键的对象。当 Boolean(value)
返回 false
时,值被视为 假 值。
你可以假设 obj
是 JSON.parse
的输出结果。换句话说,它是有效的 JSON。
\n\n
示例 1:
\n\n\n输入:obj = [null, 0, false, 1]\n输出:[1]\n解释:数组中的所有假值已被移除。\n\n\n
示例 2:
\n\n\n输入:obj = {\"a\": null, \"b\": [false, 1]}\n输出:{\"b\": [1]}\n解释:obj[\"a\"] 和 obj[\"b\"][0] 包含假值,因此被移除。\n\n
示例 3:
\n\n\n输入:obj = [null, 0, 5, [0], [false, 16]]\n输出:[5, [], [16]]\n解释:obj[0], obj[1], obj[3][0], 和 obj[4][0] 包含假值,因此被移除。\n\n\n
\n\n
提示:
\n\nobj 是一个有效的 JSON 对象
2 <= JSON.stringify(obj).length <= 106
\\u7248\\u672c\\uff1a \\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\" TypeScript 4.5.4<\\/p>\\r\\n\\r\\n Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "[null, 0, false, 1]\n{\"a\": null, \"b\": [false, 1]}\n[null, 0, 5, [0], [false, 16]]",
"__typename": "QuestionNode"
}
}
}Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n
--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n