JavaScript Dict/Object Map

Mar 10, 2020
let items = {    1: 'One',    2: 'Two',    3: 'Three'}
Object.keys(items).map((key, index) => {    items[key] = `${items[key]} is ${key}`})// orfor (key in items) {    items[key] = `${items[key]} is ${key}`}console.log(items) // {1: "One is 1", 2: "Two is 2", 3: "Three is 3"}

or create new items (instead if replacing existing ones)

const newItems = Object.keys(items).reduce((result, key) => {    result[key] = `${items[key]} is ${key}`    return result}, {})

Or use lodash.mapValues.

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.