0

Im writing a small application i Angular2, and need a simple way to convert a list to a dict.

I have a list:

var list = ["foo", "bar"];

I want to convert the list to a dict like this:

var dict = {foo: "foo", bar: "bar"};

Whats the easiest way to do this using typescript, javascript, jQuery or Angular2?

Vingtoft
  • 11,443
  • 17
  • 68
  • 114

2 Answers2

1

Try with Array#reduce

var list = ["foo", "bar"];
console.log(list.reduce((a,b)=> (a[b]=b , a), {}))
prasanth
  • 21,342
  • 4
  • 27
  • 50
1
let list = ["foo", "bar"];

        let dict = {};
        for (let i=0; i< list.length; i++ ) {
                dict[list[i]] = list[i];
        }
        console.log(dict);
Amit Chigadani
  • 25,843
  • 11
  • 74
  • 92