{ "data": { "question": { "questionId": "2804", "questionFrontendId": "2705", "boundTopicId": null, "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": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Medium", "likes": 16, "dislikes": 1, "isLiked": null, "similarQuestions": "[]", "exampleTestcases": "[null, 0, false, 1]\n{\"a\": null, \"b\": [false, 1]}\n[null, 0, 5, [0], [false, 16]]", "categoryTitle": "JavaScript", "contributors": [], "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\": \"442\", \"totalSubmission\": \"603\", \"totalAcceptedRaw\": 442, \"totalSubmissionRaw\": 603, \"acRate\": \"73.3%\"}", "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, "enableTestMode": false, "enableDebugger": false, "envInfo": "{\"javascript\": [\"JavaScript\", \"

Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES6 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\\r\\n\\r\\n

For Priority Queue / Queue data structures, you may use 5.3.0 version of datastructures-js/priority-queue and 4.2.1 version of datastructures-js/queue.

\"], \"typescript\": [\"Typescript\", \"

TypeScript 4.5.4, Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES2020 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }