UNPKG

@hitachivantara/uikit-react-core

Version:

Core React components for the NEXT Design System.

44 lines (43 loc) 1.25 kB
const searchInCollection = (collection, childrenPropName, propName, propValue) => { if (collection[propName] === propValue) { return collection; } const items = collection[childrenPropName] || collection; let foundItem = null; for (let index = 0; foundItem === null && index < items.length; index += 1) { foundItem = searchInCollection( items[index], childrenPropName, propName, propValue ); } return foundItem; }; const getNavigationItemById = (navigationItems, navigationItemId) => { return searchInCollection(navigationItems, "data", "id", navigationItemId); }; const getParentItemById = (navigationItems, navigationItemId) => { const parentId = getNavigationItemById( navigationItems, navigationItemId )?.parent; return getNavigationItemById(navigationItems, parentId); }; const fillDataWithParentId = (navigationItems, parentItemId) => { return navigationItems.map((item) => { if (item?.data?.length > 0) { return { ...item, parent: parentItemId, data: fillDataWithParentId(item.data, item.id) }; } return { ...item, parent: parentItemId }; }); }; export { fillDataWithParentId, getNavigationItemById, getParentItemById };