Create a map with the same values ​​and keys using the FP method using ES6 / Harmony

Given a set of "Apple", "Banana" and "Orange", create the following:

{ "Apple": "Apple", "Banana": "Banana", "Orange": "Orange" }

that is, each row becomes a key as well as a value.

(That is, by the way, what Facebook has little KeyMirror JS utility does in its Flux examples.)

Is there a functional programming one-liner way using ES6 / Harmony like:

["Apple", "Banana", "Orange"].map(v => v: v)

not as a non-functional programming way, for example:

let o = {}; for (let v of ["Apple", "Banana", "Orange"]) o[v] = v;

+3


source to share


1 answer


how about this, .reduce(function(obj, str){obj[str] = str; return obj;}, {})

eg:



var arry = ["Apple", "Banana", "Orange"];
var map = arry.reduce(function(obj, str){obj[str] = str; return obj;}, {});

      

+2


source







All Articles