{ "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.

\n\n

You may assume the obj is the output of JSON.parse. In other words, it is valid JSON.

\n\n

 

\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\n\n", "translatedTitle": "精简对象", "translatedContent": "

现给定一个对象或数组 obj,返回一个 精简对象精简对象 与原始对象相同,只是将包含 值的键移除。该操作适用于对象及其嵌套对象。数组被视为索引作为键的对象。当 Boolean(value) 返回 false 时,值被视为 值。

\n\n

你可以假设 objJSON.parse 的输出结果。换句话说,它是有效的 JSON。

\n\n

 

\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\n\n", "isPaidOnly": false, "difficulty": "Medium", "likes": 0, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": false}", "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {Object} obj\n * @return {Object}\n */\nvar compactObject = function(obj) {\n \n};", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "type Obj = Record;\n\nfunction compactObject(obj: Obj): Obj {\n\n};", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"89\", \"totalSubmission\": \"115\", \"totalAcceptedRaw\": 89, \"totalSubmissionRaw\": 115, \"acRate\": \"77.4%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "[null, 0, false, 1]", "metaData": "{\n \"name\": \"compactObject\",\n \"params\": [\n {\n \"name\": \"obj\",\n \"type\": \"string\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "envInfo": "{\"javascript\":[\"JavaScript\",\"

\\u7248\\u672c\\uff1aNode.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n

\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a --harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/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>\"],\"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" } } }