优化数组一个对象到 select 管理员休息
optimized array an object to a select admin on rest
如何对数组执行 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 }
}
如果您想将项目动态添加到 select,有几种方法,但对于您的上下文,这会起作用:
Html :
<select label='Día de corte' id='cutOffDay'/>
Javascript :
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' }
]
const options = choices.map(c => { return `<option id=${c.id} value=${c.name}>${c.name}</option>`})
document.getElementById('cutOffDay').innerHTML = options.join();
您必须使用 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()} />
如何对数组执行 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 }
}
如果您想将项目动态添加到 select,有几种方法,但对于您的上下文,这会起作用:
Html :
<select label='Día de corte' id='cutOffDay'/>
Javascript :
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' }
]
const options = choices.map(c => { return `<option id=${c.id} value=${c.name}>${c.name}</option>`})
document.getElementById('cutOffDay').innerHTML = options.join();
您必须使用 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()} />