I have a list of lists, each inner list has 2 items. I want to transform it into a dictionary.
const have = [['a', 1], ['b', 2]]
const want = {'a': 1, 'b': 2}
In python I would do
>>> dict([['a', 1], ['b', 2]])
{'a': 1, 'b': 2}
What is the easiest way (1-liner) to achieve this in JavaScript?
The easiest way I can think of is a 2-liner.
const have = [['a', 1], ['b', 2]]
const want = {}
have.forEach(([key, value]) => want[key] = value)
new Map([iterable])is what you are looking for here. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…JSON.stringify()that. Is there an easy way to turn it into a dict?Array.prototype.reduceas well. That will work for you.