Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function cityMarkets(arr) {
- const markets = {};
- arr.forEach((str) => {
- const [town, product, amountOfSales, priceForOneUnit] = str.split(/[^A-z0-9.\s+]+/g);
- const salesIncome = Number(amountOfSales) * Number(priceForOneUnit);
- if (!Object.prototype.hasOwnProperty.call(markets, town)) {
- markets[town] = [];
- const obj = { product: product.trim(), money: salesIncome };
- markets[town].push(obj);
- } else {
- const obj = { product: product.trim(), money: salesIncome };
- markets[town].push(obj);
- }
- });
- Object.entries(markets).forEach((town) => {
- const [townName, products] = town;
- console.log(`Town - ${townName}`);
- products.forEach((store) => {
- const { product, money } = store;
- console.log(`$$$${product} : ${money}`);
- });
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement