|
| 1 | +# [2675. Array of Objects to Matrix](https://leetcode.cn/problems/array-of-objects-to-matrix) |
| 2 | + |
| 3 | +[English Version](/solution/2600-2699/2675.Array%20of%20Objects%20to%20Matrix/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>Write a function that converts an array of objects <code>arr</code> into a matrix <code>m</code>.</p> |
| 10 | + |
| 11 | +<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> |
| 12 | + |
| 13 | +<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> |
| 14 | + |
| 15 | +<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> |
| 16 | + |
| 17 | +<p>The colums in the matrix should be in <strong>lexographically ascending</strong> order.</p> |
| 18 | + |
| 19 | +<p> </p> |
| 20 | +<p><strong class="example">Example 1:</strong></p> |
| 21 | + |
| 22 | +<pre> |
| 23 | +<strong>Input:</strong> |
| 24 | +arr = [ |
| 25 | + {"b": 1, "a": 2}, |
| 26 | + {"b": 3, "a": 4} |
| 27 | +] |
| 28 | +<strong>Output:</strong> |
| 29 | +[ |
| 30 | + ["a", "b"], |
| 31 | + [2, 1], |
| 32 | + [4, 3] |
| 33 | +] |
| 34 | + |
| 35 | +<strong>Explanation:</strong> |
| 36 | +There are two unique column names in the two objects: "a" and "b". |
| 37 | +"a" corresponds with [2, 4]. |
| 38 | +"b" coresponds with [1, 3]. |
| 39 | +</pre> |
| 40 | + |
| 41 | +<p><strong class="example">Example 2:</strong></p> |
| 42 | + |
| 43 | +<pre> |
| 44 | +<strong>Input:</strong> |
| 45 | +arr = [ |
| 46 | + {"a": 1, "b": 2}, |
| 47 | + {"c": 3, "d": 4}, |
| 48 | + {} |
| 49 | +] |
| 50 | +<strong>Output:</strong> |
| 51 | +[ |
| 52 | + ["a", "b", "c", "d"], |
| 53 | + [1, 2, "", ""], |
| 54 | + ["", "", 3, 4], |
| 55 | + ["", "", "", ""] |
| 56 | +] |
| 57 | + |
| 58 | +<strong>Explanation:</strong> |
| 59 | +There are 4 unique column names: "a", "b", "c", "d". |
| 60 | +The first object has values associated with "a" and "b". |
| 61 | +The second object has values associated with "c" and "d". |
| 62 | +The third object has no keys, so it is just a row of empty strings. |
| 63 | +</pre> |
| 64 | + |
| 65 | +<p><strong class="example">Example 3:</strong></p> |
| 66 | + |
| 67 | +<pre> |
| 68 | +<strong>Input:</strong> |
| 69 | +arr = [ |
| 70 | + {"a": {"b": 1, "c": 2}}, |
| 71 | + {"a": {"b": 3, "d": 4}} |
| 72 | +] |
| 73 | +<strong>Output:</strong> |
| 74 | +[ |
| 75 | + ["a.b", "a.c", "a.d"], |
| 76 | + [1, 2, ""], |
| 77 | + [3, "", 4] |
| 78 | +] |
| 79 | + |
| 80 | +<strong>Explanation:</strong> |
| 81 | +In this example, the objects are nested. The keys represent the full path to each value separated by periods. |
| 82 | +There are three paths: "a.b", "a.c", "a.d". |
| 83 | +</pre> |
| 84 | + |
| 85 | +<p><strong class="example">Example 4:</strong></p> |
| 86 | + |
| 87 | +<pre> |
| 88 | +<strong>Input:</strong> |
| 89 | +arr = [ |
| 90 | + [{"a": null}], |
| 91 | + [{"b": true}], |
| 92 | + [{"c": "x"}] |
| 93 | +] |
| 94 | +<strong>Output:</strong> |
| 95 | +[ |
| 96 | + ["0.a", "0.b", "0.c"], |
| 97 | + [null, "", ""], |
| 98 | + ["", true, ""], |
| 99 | + ["", "", "x"] |
| 100 | +] |
| 101 | + |
| 102 | +<strong>Explanation:</strong> |
| 103 | +Arrays are also considered objects with their keys being their indices. |
| 104 | +Each array has one element so the keys are "0.a", "0.b", and "0.c". |
| 105 | +</pre> |
| 106 | + |
| 107 | +<p><strong class="example">Example 5:</strong></p> |
| 108 | + |
| 109 | +<pre> |
| 110 | +<strong>Input:</strong> |
| 111 | +arr = [ |
| 112 | + {}, |
| 113 | + {}, |
| 114 | + {}, |
| 115 | +] |
| 116 | +<strong>Output:</strong> |
| 117 | +[ |
| 118 | + [], |
| 119 | + [], |
| 120 | + [], |
| 121 | + [] |
| 122 | +] |
| 123 | + |
| 124 | +<strong>Explanation:</strong> |
| 125 | +There are no keys so every row is an empty array.</pre> |
| 126 | + |
| 127 | +<p> </p> |
| 128 | +<p><strong>Constraints:</strong></p> |
| 129 | + |
| 130 | +<ul> |
| 131 | + <li><code>1 <= arr.length <= 1000</code></li> |
| 132 | + <li><code>unique keys <= 1000</code></li> |
| 133 | +</ul> |
| 134 | + |
| 135 | +## 解法 |
| 136 | + |
| 137 | +<!-- 这里可写通用的实现逻辑 --> |
| 138 | + |
| 139 | +<!-- tabs:start --> |
| 140 | + |
| 141 | +### **TypeScript** |
| 142 | + |
| 143 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 144 | + |
| 145 | +```ts |
| 146 | + |
| 147 | +``` |
| 148 | + |
| 149 | +<!-- tabs:end --> |
0 commit comments