首页 > 解决方案 > 优化数组一个对象到休息时选择管理员

问题描述

我怎样才能对数组做一个 for ?并且不按照我的方式去做并不是最优的,因为它是一种手动方法,我想对其进行优化

const choices = [
  { id: '1', name: '1' },
  { id: '2', name: '2' },
  { id: '3', name: '3' },
  { id: '4', name: '4' },
  { id: '5', name: '5' },
  { id: '6', name: '6' },
  { id: '7', name: '7' },
  { id: '8', name: '8' },
  { id: '9', name: '9' },
  { id: '10', name: '10' },
  { id: '11', name: '11' },
  { id: '12', name: '12' },
  { id: '13', name: '13' },
  { id: '14', name: '14' },
  { id: '15', name: '15' },
  { id: '16', name: '16' },
  { id: '17', name: '17' },
  { id: '18', name: '18' },
  { id: '19', name: '19' },
  { id: '20', name: '20' },
  { id: '21', name: '21' },
  { id: '22', name: '22' },
  { id: '23', name: '23' },
  { id: '24', name: '24' },
  { id: '25', name: '25' },
  { id: '26', name: '26' },
  { id: '27', name: '29' },
  { id: '28', name: '28' }
]
<SelectInput label='Día de corte' source='cutOffDay'
        choices={choices} />

我想做这样的事情

const choices = []
const array = (choices) => {
  for(i = 0; i < 28; i++){
  choices = { id: i, name: i }
}

标签: javascriptreactjsadmin-on-rest

解决方案


您必须使用该push方法为数组中的每个索引添加一个对象

const getChoices = () => {
    const choices = []
  for(let i = 1; i <= 28; i++){
    choices.push({ id: i, name: i });
  }
  return choices;
}

<SelectInput label='Día de corte' source='cutOffDay'
        choices={getChoices()} />

推荐阅读