Write a function that converts an array of objects arr into a matrix m.
寫一個函式,將陣列連接物件的 arr 轉換成矩陣 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.
arr 是一個含有物件的陣列,或是單純的陣列。 陣列中的每一個項目都可以是子陣列或子物件,它還可以包含數字、字符串、布爾值和空值。
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 “.”.
第一行 m 應該是列位名稱。 如果沒有巢狀,則列位名稱是物件中唯一的 keys。 如果存在巢狀,則列位名稱是物件中的各個路徑,並以「.」分隔。
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.
剩餘的每一行對應於 arr 中的一個物件,矩陣中的每個值對應於物件中的一個值。 如果給定物件不包含給列為名稱的值,則單元格應包含空字符串「""」。 矩陣中的列為名稱應按照升冪排列。
Example 1:
Input: arr = [ {"b": 1, "a": 2}, {"b": 3, "a": 4} ] Output: [ ["a", "b"], [2, 1], [4, 3] ]
Example 2:
Input: arr = [ {"a": 1, "b": 2}, {"c": 3, "d": 4}, {} ] Output: [ ["a", "b", "c", "d"], [1, 2, "", ""], ["", "", 3, 4], ["", "", "", ""] ]
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] ]
Example 4:
Input: arr = [ [{"a": null}], [{"b": true}], [{"c": "x"}] ] Output: [ ["0.a", "0.b", "0.c"], [null, "", ""], ["", true, ""], ["", "", "x"] ]
Example 5:
Input: arr = [ {}, {}, {}, ] Output: [ [], [], [], [] ]
solution:
建立一個 jsonToMatrix 函式,負責將一個包含 JSON 對象的陣列轉換成矩陣的形式。
1. 宣告一個空的 Set() 物件叫做 keySet,用來存放所有出現的 key(不重複)。 2. 使用遞迴函式將題目給的 arr 依序遍歷後添加到 keySet 中。 3. 建立 keys 為運用 Array.form().sort() 後將 keySet 轉成陣列後排序。 4. 建立一個陣列 res,並放入 keys。 5. 透過 for 迴圈來遍歷 arr 中的每個 obj 元素,並用遞迴的方式取得數值,並放入 keyToVal 物件中, 如果 keyToVal 不存在值,則添加空字串。最後將 row 推到 res 中,並回傳 res。 - getKeys(obj, path): 透過呼叫 JSON 物件中的 key,如後面仍有物件,則做 a.b 的處理。 - getValues(obj, path, keyToVal): 呼叫 JSON 物件中的 value,如後面仍有物件,則做 a.b 的處理。 - isObject(obj): 用於檢查是否為空物件,因 null 的型別為 object,故作 obj !== null && typeof obj === "object" 嚴謹的辨識。
Code 1: Matrix BigO(n)
var jsonToMatrix = function(arr) { const keySet = new Set() for (const obj of arr) { getKeys(obj, "") } const keys = Array.from(keySet).sort() const res = [keys] for (const obj of arr) { const keyToVal = {} getValues(obj, "", keyToVal) let row = [] for (const key of keys) { if (key in keyToVal) { row.push(keyToVal[key]) } else { row.push("") } } res.push(row) } return res; function getKeys(obj, path) { for (const key in obj) { const newPath = path ? `${path}.${key}` : key if (isObject(obj[key])) { getKeys(obj[key], newPath) } else { keySet.add(newPath) } } } function getValues(obj, path, keyToVal) { for (const key in obj) { const newPath = path ? `${path}.${key}` : key if (isObject(obj[key])) { getValues(obj[key], newPath, keyToVal) } else { keyToVal[newPath] = obj[key] } } } function isObject(obj) { return obj !== null && typeof obj === "object" } };
FlowChart:
Example 1
console.log(jsonToMatrix([ {"b": 1, "a": 2}, {"b": 3, "a": 4} ])) // Output: // [ // ["a", "b"], // [2, 1], // [4, 3] // ]
Example 2
console.log(jsonToMatrix([ {"a": 1, "b": 2}, {"c": 3, "d": 4}, {} ])) // Output: // [ // ["a", "b", "c", "d"], // [1, 2, "", ""], // ["", "", 3, 4], // ["", "", "", ""] // ]
Example 3
console.log(jsonToMatrix([ {"a": {"b": 1, "c": 2}}, {"a": {"b": 3, "d": 4}} ])) // Output: // [ // ["a.b", "a.c", "a.d"], // [1, 2, ""], // [3, "", 4] // ]
Example 4
console.log(jsonToMatrix([ [{"a": null}], [{"b": true}], [{"c": "x"}] ])) // Output: // [ // ["0.a", "0.b", "0.c"], // [null, "", ""], // ["", true, ""], // ["", "", "x"] // ]
Example 5
console.log(jsonToMatrix([ {}, {}, {}, ])) // Output: // [ // [], // [], // [], // [] // ]