0

i am stumbling onto something that should be easy, yet i can't seem to be able to do it.

i have an array of string, and i want to map over it to create an object like so :

const monthesList = ['january', 'february', 'march', 'april', 'may']

const myState = monthesList.map(month => (
    month: {selected: false, year: 2022}
    )
  )

what i want is to get the following object :

myState ={
  january:{ 
    selected: false,
    year: 2022
  },
  february:{ 
    selected: false,
    year: 2022
  },
  march:{ 
    selected: false,
    year: 2022
  },
  april:{ 
    selected: false,
    year: 2022
  },
  may:{ 
    selected: false,
    year: 2022
  },
}

Edit: i just found the way :

const myState = monthesList.map(month => (
    {[month] : {
      selected: false,
      year: 2022
    }}
    )
  )

just need to make an object out of it, shouldn't be too hard

4
  • yes you're right, i know but it's not what was bothering me, i can easliy transfomr the array to an object Commented Jun 9, 2022 at 7:36
  • Could you please edit your question to match your examples to your code; is it selected or sleected? Is it true or false? Commented Jun 9, 2022 at 7:44
  • You still have selected: true in your first code snippet and false everywhere else Commented Jun 9, 2022 at 7:54
  • sorry about that, it's fixed ! Commented Jun 9, 2022 at 8:24

3 Answers 3

2

The .map() method returns an array, but the output that you're after is an object. You need something that can convert an array of key-value pairs (ie: [key, value]) to an object. For this, we have the Object.fromEntries() method that takes an array of key-value pairs, and from it, constructs an object like so:

const monthesList = ['january', 'february', 'march', 'april', 'may'];

const myState = Object.fromEntries(monthesList.map(month => [
  month, {selected: false, year: 2022}
]));
console.log(myState);

Sign up to request clarification or add additional context in comments.

Comments

2

You can use map to generate an array of arrays with the values in each internal array being the month name and object; then use Object.fromEntries to convert that into your desired object:

const monthesList = ['january', 'february', 'march', 'april', 'may']

const myState = Object.fromEntries(
  monthesList.map(m => [m, {selected: false, year: 2022}])
)

console.log(myState)

6 Comments

Small fyi: selected should be false.
It should also be sleected according to OP's example 😉
@Andy Ironically I copied it from OPs code...
@Phil well, the spelling has been corrected but there's still one true and one false in the question's code... perhaps I should put in a non-binary value instead :)
|
1

map will only return an array. Your expected output is an object. There's no need to over-complicate things - use a simple loop instead.

const list = ['january', 'february', 'march', 'april', 'may'];

const myState = {};

for (const month of list) {
  myState[month] = { selected: false, year: 2022 };
}

console.log(myState);

Comments

Your Answer

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.