const l = ["ABART 20", "CDXEF 50", "BKWRK 25", "BTSQZ 89", "DRTYM 60"]; const m = ["A", "B", "C", "W"]; const store = l.reduce((acc, inventory) => { const [[code], qty] = inventory.split(" "); const stock = (acc[code] || 0) + Number(qty); return { ...acc, [code]: stock } }, {}); const counts = m.reduce((result, key) => ({ ...result, [key]: store[key] || 0 }), {}) const print = obj => Object.entries(obj) .map(([k, v]) => `(${k} : ${v})`) .join(" - "); console.log(print(counts));
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.
JavaScript, with map ( for constant lookups )