Generation

generate functionTue, 13 Jun 2023

return an object that groups all the child keys in an array by parent key

const createParentChildTree = (parentChildKeyArray) => { const parent = []; const children = []; const result = { children, parent }; parentChildKeyArray.forEach(([parentKey, childKey]) => { if (!result.parent.includes(parentKey)) { result.parent.push(parentKey); } if (!result.children[parentKey]) { result.children[parentKey] = []; } result.children[parentKey].push(childKey); }); return result; };

Javascript
Generate More

Questions about programming?Chat with your personal AI assistant