0

I have an array, say

var arr = ["id", "currency", "region", "status"]

How can I map the above to an object literal, as below?

var columns = [
 { key:"id", headerRenderer:"id" },
 { key:"currency", headerRenderer:"currency" },
 { key:"status", headerRenderer:"region" },
 { key:"region", headerRenderer:"status" }
 ];

Thanks in advance!

Mihai Alexandru-Ionut
48.6k14 gold badges106 silver badges132 bronze badges
asked Jan 26, 2017 at 11:25
2
  • var 1 is a syntax error. Commented Jan 26, 2017 at 11:27
  • No, I flipped them by mistake. And thanks for the tip with var, Commented Jan 26, 2017 at 11:28

4 Answers 4

4

One solution is to use Array#map method.

The map() method creates a new array with the results of calling a provided function on every element in this array.The provided function is a callback.

So, as I said above map method provided callback function once for each element in an array, in order, and constructs a new array from the results. The elements from the result array are objects, like this: {"key":item, "headerRenderer":item}.

var array = ["id", "currency", "region", "status"]
var columns=array.map(function(item){
 return {
 "key":item,
 "headerRenderer":item
 }
});
console.log(columns);

answered Jan 26, 2017 at 11:27
Sign up to request clarification or add additional context in comments.

Comments

3

The map method on the Array prototype allows you to apply a function to every element in an array, thereby effectively abstracting iteration for simple cases. Your case is simple, because the content of any entry in result is completely specified by its corresponding entry in arr, which means you can "map"

"id" --> { key: "id", headerRenderer: "id" }

Using map you can solve your problem in just one line (provided your environment supports ES6):

var arr = ["id", "currency", "region", "status"]
var result = arr.map(key => ({ key, headerRenderer: key }));
console.log(result);

answered Jan 26, 2017 at 11:31

Comments

2
var columns = [];
var array = ["id", "currency", "region", "status"];
for (var i = 0, len = array.length; i < len; i++) {
 columns.push({
 key: array[i],
 headerRenderer: array[i]
 });
}
answered Jan 26, 2017 at 11:28

Comments

1

Normally .map() is the way but another functional approach would be to use a tail call recursive map as follows;

var arr = ["id", "currency", "region", "status"],
 mapo = (a, r=[]) => a.length ? mapo(a.slice(1),r.concat({key:a[0],headerRenderer:a[0]})) : r;
console.log(mapo(arr));

answered Jan 26, 2017 at 11:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.