[英]reduce array of objects into nested arrays
我試圖在彼此之間嵌套元素,但是今天我卻放屁了。
如果數組中的元素為isParent:true,則它將在exist數組中創建一個新數組,並且所有后續元素都將嵌套在該數組中。
到目前為止,這是我嘗試過的:
//input [{},{},{},{isParent}, {}, {}, {isParent}, {}, {}] //desired output [{}, {}, {}, [{}, {}, {}, [{}, {}, {}]]] var nestInParent = elements => { let ignoreIndexAfter = null; return elements.reduce((acc, { component, isParent }, index) => { if (isParent) { let remaining = elements.slice(index + 1); ignoreIndexAfter = index; if (remaining.length > 0) { return [...acc, [component, ...nestInParent(remaining)]]; } else { return [...acc, [component]]; } } else { if(ignoreIndexAfter === null || index < ignoreIndexAfter){ return [...acc, component]; } return acc; } }, []); }; const isParent = true; const input = [ {component:0}, {component:1}, {component:2}, {isParent, component:3}, {component:4}, {component:5}, {isParent, component:6}, {component:7}, {component:8} ]; const expected = [ 0, 1, 2, [ 3, 4, 5, [ 6, 7, 8 ] ] ]; const output = nestInParent(input); console.log("input:", input); console.log("output:", output); console.log("passes?", JSON.stringify(output) === JSON.stringify(expected));
.as-console-wrapper { max-height: 100% !important; }
您也可以使用reduce
方法和一個變量來跟蹤是否找到帶有isParent
元素。
const isParent = true; const input = [{component:0},{component:1},{component:2},{isParent, component:3},{component:4},{component:5},{isParent, component:6},{component:7},{component:8}]; function nest(data) { let nested = false; return data.reduce((r, e, i) => { if(!nested) { if(e.isParent) { const res = nest(data.slice(i + 1)) r.push([e.component, ...res]) nested = true; } else { r.push(e.component) } } return r; }, []) } const result = nest(input); console.log(result)
您也可以這樣寫。
const isParent = true; const input = [{ component: 0 }, { component: 1 }, { component: 2 }, { isParent, component: 3 }, { component: 4 }, { component: 5 },{ isParent, component: 6 }, { component: 7 }, { component: 8 }]; function nest(data, nested) { return data.reduce((r, e, i) => { if(!nested) { if(e.isParent && i != 0) { r.push(nest(data.slice(i))) nested = true; } else { r.push(e.component) } } return r; }, []) } const result = nest(input); console.log(result)
我會說您的代碼失敗,因為它過於復雜。 您只需要:
const result = [];
let current = result;
for(const { component, isParent } of data) {
if(isParent) {
current.push(current = [component]);
} else current.push(component);
}
const input = [ {component:0}, {component:1}, {component:2}, {isParent: true, component:3}, {component:4}, {component:5}, {isParent: true, component:6}, {component:7}, {component:8} ]; const result = []; let current = result; for(const { component, isParent } of input) { if(isParent) { current.push(current = [component]); } else current.push(component); } console.log(result);
或者,如果您確實要減少,請減少right:
const result = data.reduceRight((acc, { isParent, component }) => isParent ? [[component, ...acc]] : [component, ...acc], []);
const input = [ {component:0}, {component:1}, {component:2}, {isParent: true, component:3}, {component:4}, {component:5}, {isParent: true, component:6}, {component:7}, {component:8} ]; const result = input.reduceRight((acc, { isParent, component }) => isParent ? [[component, ...acc]] : [component, ...acc], []); console.log(result);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.