我需要一般情況的解決方案
例如
let data = [['a', 'b'],['c', 'd'],['e', 'f', 'g', 'h']];
我需要這個:
{
"a": {
"c": {
"e": 0,
"f": 0,
"g": 0,
"h": 0
},
"d": {
"e": 0,
"f": 0,
"g": 0,
"h": 0
}
},
"b": {
"c": {
"e": 0,
"f": 0,
"g": 0,
"h": 0
},
"d": {
"e": 0,
"f": 0,
"g": 0,
"h": 0
}
}
}
和資料可以是任何隨機陣列陣列......我嘗試了一種遞回方法,但我陷入了 Map 和 .fromEntries 方法......
uj5u.com熱心網友回復:
簡單的遞回:
- 基本情況 - 我們在陣列中只有一個陣列。我們用它的默認值構造一個物件。
- 遞回步驟 - 我們有更多的陣列。我們建立了一個物件,每個鍵都來自第一個陣列,每個值都是使用其余陣列的遞回呼叫:
const buildObj = (data, defaultValue = 0) => {
if (data.length > 1)
return Object.fromEntries(
data[0].map(x => [x, buildObj(data.slice(1), defaultValue)])
)
return Object.fromEntries(
data[0].map(x => [x, defaultValue])
);
}
console.log(buildObj([
['e', 'f', 'g', 'h']
]));
console.log(buildObj([
['c', 'd'],
['e', 'f', 'g', 'h']
]));
console.log(buildObj([
['a', 'b'],
['c', 'd'],
['e', 'f', 'g', 'h']
]));
console.log(buildObj([
['a', 'b'],
['c', 'd'],
['e', 'f', 'g', 'h']
], 42)); //different default
也可以表示為:
- 基本情況 - 回傳默認值。
- 遞回步驟 - 我們建立一個物件,每個鍵來自第一個陣列,每個值都是使用其余陣列的遞回呼叫。
const buildObj = (data, defaultValue = 0) => {
if (data.length !== 0)
return Object.fromEntries(
data[0].map(x => [x, buildObj(data.slice(1), defaultValue)])
);
return defaultValue;
}
console.log(buildObj([
['e', 'f', 'g', 'h']
]));
console.log(buildObj([
['c', 'd'],
['e', 'f', 'g', 'h']
]));
console.log(buildObj([
['a', 'b'],
['c', 'd'],
['e', 'f', 'g', 'h']
]));
console.log(buildObj([
['a', 'b'],
['c', 'd'],
['e', 'f', 'g', 'h']
], 42)); //different default
uj5u.com熱心網友回復:
我認為這運行良好。使用 step 運行遞回。如果需要,您可以更改forEach為for回圈。
let data = [['a', 'b'],['c', 'd'],['e', 'f', 'g', 'h']];
const arrToDict = (data) => {
const recursive = (depth = 0) => {
let dict = {}
if (data.length === depth 1) {
data[depth].forEach(el => {
dict[el] = 0
})
} else {
data[depth].forEach(el => {
dict[el] = recursive(depth 1)
})
}
return dict
}
return recursive();
}
arrToDict(data)
uj5u.com熱心網友回復:
我使用遞回將每個索引轉換為物件,然后使用Memoization來獲得更有效的解決方案
基本情況:當索引超出當前陣列的邊界時 遞回情況:遞回下一個索引并將下一個物件化索引作為值分配給當前鍵
//store the index which is already iterated/objectified
//this is just for LESS COMPUTATION and MORE EFFICIENCY
const memoizeObjectifiedIndex = {};
let data = [['a', 'b'],['c', 'd'],['e', 'f', 'g', 'h']];
//basic recursive approach
function createObject(data,index){
//base case,
//if the index is just outside the length of array,
//here that index=3, since array is 0 indexed and last index is 2
if(index === data.length) return 0;
//check in memoized object if current index is already objectfied
if(memoizeObjectifiedIndex[index]){
//you can check the hits when this condition is true and COMPUTATION is saved
// console.log("Found for index ", index);
return memoizeObjectifiedIndex[index];}
const obj={};
data[index].forEach((key) => {
//assign the next objectified index as value to current key
obj[key] = createObject(data,index 1);
})
//store the object for current index for future use
memoizeObjectifiedIndex[index] = obj;
return obj;
}
console.log(createObject(data,0))
注意:通過在瀏覽器控制臺中復制和運行此代碼,您可以看到更好的輸出。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/364885.html
標籤:javascript 数组 目的
