Moment.js.get weekdays as array with keys

Currently in moment.js there is function moment.weekdays() which returns you the array like: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

The issue for me that it does not have keys.

I’ve thought about something like this, but not quite sure if it’s correct solution:

const array = moment.weekdays()
const newArr2 = array.map(v => Object.assign(v, {isActive: true}))

The result which I wish to get:

[["0", "Sunday"], ["1", "Monday"] ["2", "Tuesday"], ["3", "Wednesday"], ["4", "Thursday"], ["5", "Friday"], ["6", "Saturday"]]

Answer

Update after new information – Note I take your request literally although I think you have an X/Y problem

The result I wish to get is [[“0”, “Sunday”], [“1”, “Monday”] [“2”, “Tuesday”], [“3”, “Wednesday”], [“4”, “Thursday”], [“5”, “Friday”], [“6”, “Saturday”]]

const array = moment.weekdays()
const newArr2 = array.map((v,i) => [String(i),v])
console.log(newArr2)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous"></script>

A select:

populateOptions(options) {
  return moment.weekdays().map((option, index) => (
    <option key={index} value={option}>{option}</option>
  ));
}

old answer:

array[today.getDay()]

or do you mean something like this

const today = moment();
const dow = today.day();

const array = moment.weekdays()
const newArr2 = array.map(v => ({[v]: { isActive: true}}))
console.log(newArr2[dow])
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous"></script>

Leave a Reply

Your email address will not be published. Required fields are marked *