{ "data": { "question": { "questionId": "2769", "questionFrontendId": "2675", "boundTopicId": null, "title": "Array of Objects to Matrix", "titleSlug": "array-of-objects-to-matrix", "content": "
Write a function that converts an array of objects arr
into a matrix m
.
arr
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.
The first row m
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 "."
.
Each of the remaining rows corresponds to an object in arr
. 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 ""
.
The colums in the matrix should be in lexographically ascending order.
\n\n\n
Example 1:
\n\n\nInput: \narr = [\n {"b": 1, "a": 2},\n {"b": 3, "a": 4}\n]\nOutput: \n[\n ["a", "b"],\n [2, 1],\n [4, 3]\n]\n\nExplanation:\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\n\n
Example 2:
\n\n\nInput: \narr = [\n {"a": 1, "b": 2},\n {"c": 3, "d": 4},\n {}\n]\nOutput: \n[\n ["a", "b", "c", "d"],\n [1, 2, "", ""],\n ["", "", 3, 4],\n ["", "", "", ""]\n]\n\nExplanation:\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\n\n
Example 3:
\n\n\nInput: \narr = [\n {"a": {"b": 1, "c": 2}},\n {"a": {"b": 3, "d": 4}}\n]\nOutput: \n[\n ["a.b", "a.c", "a.d"],\n [1, 2, ""],\n [3, "", 4]\n]\n\nExplanation:\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\n\n
Example 4:
\n\n\nInput: \narr = [\n [{"a": null}],\n [{"b": true}],\n [{"c": "x"}]\n]\nOutput: \n[\n ["0.a", "0.b", "0.c"],\n [null, "", ""],\n ["", true, ""],\n ["", "", "x"]\n]\n\nExplanation:\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\n\n
Example 5:
\n\n\nInput: \narr = [\n {},\n {},\n {},\n]\nOutput: \n[\n [],\n [],\n [],\n []\n]\n\nExplanation:\nThere are no keys so every row is an empty array.\n\n
\n
Constraints:
\n\n1 <= arr.length <= 1000
unique keys <= 1000
Node.js 16.13.2
.
Your code is run with --harmony
flag, enabling new ES6 features.
lodash.js library is included by default.
\\r\\n\\r\\nFor 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
.
Your code is run with --harmony
flag, enabling new ES2020 features.
lodash.js library is included by default.
\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }