In javascript
const l = ["ABART 20", "CDXEF 50", "BKWRK 25", "BTSQZ 89", "DRTYM 60"]; const m = ["A", "B", "C", "W"]; const count = m.reduce((o, key) => { const c = l.reduce((total, item) => { const [code, nr] = item.split(" "); return total + parseInt(code.indexOf(key) === 0 ? nr : 0, 10); }, 0); return { ...o, [key]: c }; }, {}); const printResult = obj => { return Object.keys(obj) .map(k => `(${k} : ${obj[k]})`) .join(" - "); }; console.log(printResult(count));
CodeSandbox example
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
In javascript
CodeSandbox example