-1

I have the following array:

['2020-01-16', '2020-01-17', '2020-01-18']

I need to turn the array above into an object like this:

{
    '2020-01-16': {selected: true, marked: true, selectedColor: 'blue'},
    '2020-01-17': {selected: true, marked: true, selectedColor: 'blue'},
    '2020-01-18': {selected: true, marked: true, selectedColor: 'blue'},
}

Is there a way I can do this?

Mr PizzaGuy
  • 410
  • 6
  • 19
  • i don't think it's possible. You might need to use javascript and loops to turn the list into an object. – Mr PizzaGuy Jan 16 '20 at 00:30
  • use for loop. ex: let result = {}; array.forEach(element =>{result[element] = {selected: true, marked: true, selectedColor: 'blue'};}); console.log(result); – wakakak Jan 16 '20 at 00:33
  • 1
    Also, please include that you have researched. If you haven't, I recommend you do so next time :). – Mr PizzaGuy Jan 16 '20 at 00:35

3 Answers3

2

Sure, using Array.reduce(), this is a pretty straightforward thing. The accumulator in the reduce function is simply an empty object, and each iteration through reduce, we create a new property with that array item's value as the property name, and define the object literal as the value of that property.

Hope this helps!

const myArray = ['2020-01-16', '2020-01-17', '2020-01-18'];

const myObject = myArray.reduce( (obj, item) => {
  obj[item] = {selected: true, marked: true, selectedColor: 'blue'};
  return obj;
 }, {});
 
 console.log(JSON.stringify(myObject) );
Snowmonkey
  • 3,716
  • 1
  • 16
  • 16
1

Use .forEach() to iterate over all of them and build your object out.

var myKeys = ['2020-01-16', '2020-01-17', '2020-01-18'];
var myObject = {};

myKeys.forEach((key) => myObject[key] = {
    selected: true, 
    marked: true, 
    selectedColor: 'blue'
});

console.log(myObject);

You'll likely need to change the logic as far as the specific data you're plugging in (for selected, marked, selectedColor), but this is the simplest approach generally.

Lewis
  • 4,285
  • 1
  • 23
  • 36
  • 1
    You and I are both gonna get them -the OP hadn't actually tried anything, or hadn't offered code that wasn't working.. and we just went ahead and answered his question. ;) – Snowmonkey Jan 16 '20 at 00:38
  • 1
    Don't worry, I upvoted both of your questions, there all pretty good! Edit: They should add a list of downvoters and upvoters just for the convenience lol. – Mr PizzaGuy Jan 16 '20 at 00:39
  • 1
    wow 3 downvotes. I have no idea why. I personally prefer pure functions but its not wrong. – Paul Rooney Jan 16 '20 at 00:41
  • I'm with you, @PaulRooney - its why I prefer `reduce()` over `forEach()`. But in this instance, they both work pretty well. – Snowmonkey Jan 16 '20 at 00:42
  • Another functional way `Object.fromEntries(input.map(i => [i, x]))` where `x` is the common parameters object. `Object.fromEntries` quite a new function though. – Paul Rooney Jan 16 '20 at 00:51
1

I would use reduce to handle this (documentation on reduce):

var arr = ['2020-01-16', '2020-01-17', '2020-01-18'];

arr.reduce(function(accumulator, val){
  accumulator[val] = {selected: true, marked: true, selectedColor: 'blue'};
  return accumulator;
}, {});

This will build out your object as it loops over the array. I like using reduce over forEach, when I can

nzifnab
  • 15,876
  • 3
  • 50
  • 65