# 2675. Array of Objects to Matrix ###### tags: `leetcode 30 days js challenge` `Medium` [2675. Array of Objects to Matrix](https://leetcode.com/problems/array-of-objects-to-matrix/) ### 題目描述 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. ### 範例 **Example 1:** ``` Input: arr = [ {"b": 1, "a": 2}, {"b": 3, "a": 4} ] Output: [ ["a", "b"], [2, 1], [4, 3] ] Explanation: There are two unique column names in the two objects: "a" and "b". "a" corresponds with [2, 4]. "b" coresponds with [1, 3]. ``` **Example 2:** ``` Input: arr = [ {"a": 1, "b": 2}, {"c": 3, "d": 4}, {} ] Output: [ ["a", "b", "c", "d"], [1, 2, "", ""], ["", "", 3, 4], ["", "", "", ""] ] Explanation: There are 4 unique column names: "a", "b", "c", "d". The first object has values associated with "a" and "b". The second object has values associated with "c" and "d". The third object has no keys, so it is just a row of empty strings. ``` **Example 3:** ``` Input: arr = [ {"a": {"b": 1, "c": 2}}, {"a": {"b": 3, "d": 4}} ] Output: [ ["a.b", "a.c", "a.d"], [1, 2, ""], [3, "", 4] ] Explanation: In this example, the objects are nested. The keys represent the full path to each value separated by periods. There are three paths: "a.b", "a.c", "a.d". ``` **Example 4:** ``` Input: arr = [ [{"a": null}], [{"b": true}], [{"c": "x"}] ] Output: [ ["0.a", "0.b", "0.c"], [null, "", ""], ["", true, ""], ["", "", "x"] ] Explanation: Arrays are also considered objects with their keys being their indices. Each array has one element so the keys are "0.a", "0.b", and "0.c". ``` **Example 5:** ``` Input: arr = [ {}, {}, {}, ] Output: [ [], [], [], [] ] Explanation: There are no keys so every row is an empty array. ``` **Constraints**: - `1 <= arr.length <= 1000` - `unique keys <= 1000` ### 解答 #### TypeScript ```typescript= function jsonToMatrix(arr: any[]): (string | number | boolean | null)[][] { const flattenObject = (obj: any, prefix = ''): Record<string, any> => { let result: Record<string, any> = {}; for (let key in obj) { let value = obj[key]; if (typeof value === 'object' && value !== null) { const flattened = flattenObject(value, prefix + key + '.'); result = { ...result, ...flattened }; } else { result[prefix + key] = value; } } return result; }; const flattenedArr = arr.map((obj) => flattenObject(obj)); const columnSet = new Set<string>(); flattenedArr.forEach((obj) => { Object.keys(obj).forEach((key) => columnSet.add(key)); }); const columns = Array.from(columnSet).sort(); const matrix: (string | number | boolean | null)[][] = [columns]; flattenedArr.forEach((obj) => { const row = columns.map((key) => (obj[key] !== undefined ? obj[key] : '')); matrix.push(row); }); return matrix; } ``` Backtracking 解法: ```typescript= const flattenBacktracking = ( element: any, path: string, object: Record<string, any>, columns: Set<string> ) => { if (element != null && typeof element === 'object') { Object.entries(element).forEach(([key, value]) => { flattenBacktracking(value, path + (path ? '.' : '') + key, object, columns); }); } else { object[path] = element; columns.add(path); } return object; }; function jsonToMatrix(arr: any[]): (string | number | boolean | null)[][] { const matrix: (string | number | boolean | null)[][] = []; const columns = new Set<string>(); arr = arr.map((element) => flattenBacktracking(element, '', {}, columns)); matrix.push([...columns].sort()); const columnsIndex = matrix[0].reduce( (acc, curr, index) => ((acc[curr as string] = index), acc), {} as Record<string, number> ); arr.forEach((element) => { matrix.push(Array(columns.size).fill('')); Object.entries(element).forEach(([key, value]) => { matrix[matrix.length - 1][columnsIndex[key]] = value as any; }); }); return matrix; } ``` > [name=Sheep][time=Tues, May 23, 2023] ### Reference [回到題目列表](https://hackmd.io/@Marsgoat/leetcode_every_day)