Moment.js.get 工作日作为带键的数组

Moment.js.get weekdays as array with keys

目前在 moment.js 中有一个函数 moment.weekdays(),returns 你喜欢这样的数组:["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

我的问题是它没有钥匙。

我考虑过这样的事情,但不太确定它是否是正确的解决方案:

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

我希望得到的结果:

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

在收到新信息后更新 - 请注意,尽管我认为您有 X/Y 问题

,但我照字面意思理解您的请求

我希望得到的结果是[["0", "Sunday"], ["1", "Monday"] ["2", "Tuesday"], ["3 ", "星期三"], ["4", "星期四"], ["5", "星期五"], ["6", "星期六"]]

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>

一个select:

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

旧答案:

array[today.getDay()]

或者你的意思是这样的

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>