Fetch the repository succeeded.
{
"data": {
"question": {
"questionId": "2769",
"questionFrontendId": "2675",
"boundTopicId": null,
"title": "Array of Objects to Matrix",
"titleSlug": "array-of-objects-to-matrix",
"content": "<p>Write a function that converts an array of objects <code>arr</code> into a matrix <code>m</code>.</p>\n\n<p><code>arr</code> is an array of objects or arrays. Each item in the array can be deeply nested with child arrays and child objects. It can also contain numbers, strings, booleans, and null values.</p>\n\n<p>The first row <code>m</code> should be the column names. If there is no nesting, the column names are the unique keys within the objects. If there is nesting, the column names are the respective paths in the object separated by <code>"."</code>.</p>\n\n<p>Each of the remaining rows corresponds to an object in <code>arr</code>. Each value in the matrix corresponds to a value in an object. If a given object doesn't contain a value for a given column, the cell should contain an empty string <code>""</code>.</p>\n\n<p>The colums in the matrix should be in <strong>lexographically ascending</strong> order.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narr = [\n {"b": 1, "a": 2},\n {"b": 3, "a": 4}\n]\n<strong>Output:</strong> \n[\n ["a", "b"],\n [2, 1],\n [4, 3]\n]\n\n<strong>Explanation:</strong>\nThere are two unique column names in the two objects: "a" and "b".\n"a" corresponds with [2, 4].\n"b" coresponds with [1, 3].\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narr = [\n {"a": 1, "b": 2},\n {"c": 3, "d": 4},\n {}\n]\n<strong>Output:</strong> \n[\n ["a", "b", "c", "d"],\n [1, 2, "", ""],\n ["", "", 3, 4],\n ["", "", "", ""]\n]\n\n<strong>Explanation:</strong>\nThere are 4 unique column names: "a", "b", "c", "d".\nThe first object has values associated with "a" and "b".\nThe second object has values associated with "c" and "d".\nThe third object has no keys, so it is just a row of empty strings.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narr = [\n {"a": {"b": 1, "c": 2}},\n {"a": {"b": 3, "d": 4}}\n]\n<strong>Output:</strong> \n[\n ["a.b", "a.c", "a.d"],\n [1, 2, ""],\n [3, "", 4]\n]\n\n<strong>Explanation:</strong>\nIn this example, the objects are nested. The keys represent the full path to each value separated by periods.\nThere are three paths: "a.b", "a.c", "a.d".\n</pre>\n\n<p><strong class=\"example\">Example 4:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narr = [\n [{"a": null}],\n [{"b": true}],\n [{"c": "x"}]\n]\n<strong>Output:</strong> \n[\n ["0.a", "0.b", "0.c"],\n [null, "", ""],\n ["", true, ""],\n ["", "", "x"]\n]\n\n<strong>Explanation:</strong>\nArrays are also considered objects with their keys being their indices.\nEach array has one element so the keys are "0.a", "0.b", and "0.c".\n</pre>\n\n<p><strong class=\"example\">Example 5:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narr = [\n {},\n {},\n {},\n]\n<strong>Output:</strong> \n[\n [],\n [],\n [],\n []\n]\n\n<strong>Explanation:</strong>\nThere are no keys so every row is an empty array.</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 <= arr.length <= 1000</code></li>\n\t<li><code>unique keys <= 1000</code></li>\n</ul>\n",
"translatedTitle": null,
"translatedContent": null,
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 14,
"dislikes": 2,
"isLiked": null,
"similarQuestions": "[{\"title\": \"JSON Deep Equal\", \"titleSlug\": \"json-deep-equal\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Convert Object to JSON String\", \"titleSlug\": \"convert-object-to-json-string\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
"exampleTestcases": "[{\"b\":1,\"a\":2},{\"b\":3,\"a\":4}]\n[{\"a\":1,\"b\":2},{\"c\":3,\"d\":4},{}]\n[{\"a\":{\"b\":1,\"c\":2}},{\"a\":{\"b\":3,\"d\":4}}]\n[[{\"a\":null}],[{\"b\":true}],[{\"c\":\"x\"}]]\n[{},{},{}]",
"categoryTitle": "JavaScript",
"contributors": [],
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {Array} arr\n * @return {Matrix}\n */\nvar jsonToMatrix = function(arr) {\n \n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "function jsonToMatrix(arr: any[]): (string | number | boolean | null)[] {\n\n};",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"207\", \"totalSubmission\": \"321\", \"totalAcceptedRaw\": 207, \"totalSubmissionRaw\": 321, \"acRate\": \"64.5%\"}",
"hints": [
"How could you split the problem up into sub-problems?",
"1.) Write a function that converts a single object into a dictionary that maps the path name to values. You can solve this recursively by keeping track of current path list.",
"2.) Write a function that converts a list of dictionaries into a matrix. Start by creating a list of all possible paths in any of the dictionaries. This will represent the columns."
],
"solution": null,
"status": null,
"sampleTestCase": "[{\"b\":1,\"a\":2},{\"b\":3,\"a\":4}]",
"metaData": "{\n \"name\": \"jsonToMatrix\",\n \"params\": [\n {\n \"name\": \"arr\",\n \"type\": \"string\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"languages\": [\n \"typescript\",\n \"javascript\"\n ],\n \"manual\": true\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"enableTestMode": false,
"enableDebugger": false,
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 4.5.4, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2020 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
"libraryUrl": null,
"adminUrl": null,
"challengeQuestion": null,
"__typename": "QuestionNode"
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。