Es hora de elegir el cerebro de la mente de colmena.
Estoy creando un menú desplegable (bueno, 2 de ellos) y necesito que hagan lo siguiente Altura de 4'0 "a 7'0" Peso de 100 lb a 400 lb en incrementos de 5 lb.
¿Cuál sería la forma mejor/más fácil de crear esta matriz sin tener que crear una matriz manualmente?
Sólo tiene que ser tan simple como
const heights = [ { id: '', name: '' }, ]Simplemente no sé cómo crearlo mejor en tan pocas líneas de código o creando manualmente la matriz
Lo mismo con la altura en incrementos de 5 libras
EDITAR: PARA QUE la gente sepa POR QUÉ estoy preguntando esto: intente hacer una búsqueda en Google y disfrute de la frustración.
Para los pesos, puede usar la función Array.fill como se ve en esta respuesta .
// https://stackoverflow.com/questions/3895478/does-javascript-have-a-method-like-range-to-generate-a-range-within-the-supp const range = (start, stop, step = 1) => Array(Math.ceil((stop - start) / step) + 1).fill(start).map((x, y) => x + y * step) const weights = range(100, 500, 5).map((x, index) => ({ id: index, name: x })) console.log(weights) // or with one line of code const w = Array(Math.ceil((500 - 100) / 5) + 1).fill(100).map((x, index) => ({ name: x + index * 5, id: index })) console.log(w)Para las alturas, puede usar un algoritmo simple como un bucle while con una condición para el incremento
const start = { integer: 4, fractionnal: 0 } const end = { integer: 7, fractionnal: 0 } const heights = [] let index = 1 while (start.integer < end.integer || start.fractionnal <= end.fractionnal) { heights.push({ id: index, name: `${start.integer}.${start.fractionnal}` }) if (start.fractionnal < 11) start.fractionnal += 1 else { start.integer += 1 start.fractionnal = 0 } } console.log(heights)