woocommerce/plugins/woocommerce-blocks/assets/js/base/utils/keyby.ts

8 lines
252 B
TypeScript
Raw Normal View History

2023-04-28 10:29:45 +00:00
export const keyBy = < T >( array: T[], key: keyof T ) => {
return array.reduce( ( acc, value ) => {
const computedKey = key ? String( value[ key ] ) : String( value );
acc[ computedKey ] = value;
return acc;
}, {} as Record< string, T > );
};