Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function catalogue(args) {
- const listKeys = [];
- args.sort((a, b) => a.localeCompare(b));
- args.forEach((element) => {
- const key = element.charAt(0);
- const alphaObj = {};
- alphaObj.key = key;
- alphaObj.value = [];
- listKeys.push(alphaObj);
- });
- const filteredArr = listKeys.reduce((acc, current) => {
- const x = acc.find((item) => item.key === current.key);
- if (!x) {
- return acc.concat([current]);
- }
- return acc;
- }, []);
- args.forEach((element) => {
- const key = element.charAt(0);
- filteredArr.forEach((el) => {
- if (el.key === key) {
- el.value.push(element);
- }
- });
- });
- filteredArr.forEach((element) => {
- const productName = element.key;
- const productPrice = element.value;
- console.log(`${productName}`);
- productPrice.forEach((el) => {
- const [name, price] = el.split(':');
- name.trimEnd();
- console.log(` ${name.trimEnd()}:${price}`);
- });
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement