-3

OK time to pick the hive mind brain.

I am creating a dropdown (well 2 of them) and I need them to do the following Height from 4'0" to 7'0" Weight from 100lb to 400lb in 5lb incraments.

What would be the best/easiest way to create this array without having to manually create an array

It just needs to be as simple as

const heights = [
    { id: '', name: '' },
]

I just to not know how to best create it in as few Lines of code or manually creating the array

Same with height in 5lb increments

EDIT: SO people know WHY I am asking this - try doing a google search and enjoy the frustration.

ArcticMediaRyan
  • 687
  • 8
  • 32
  • height just goes 4'0" up to 7'0" weight needs to go 100lb to 400lb (100, 105, 110) When I google search for "Create Vue Array of height in feet" it just gives you div height stuff so its frustrating – ArcticMediaRyan Mar 31 '22 at 22:33
  • Actually I have solved it in a complicated way - well 2 ways - one pulling a list from a DB, and the other was creating the list then ripping out the data I do not require. The fact people want to sit here and come at me thinking I do not know what I am doing is laughable at best. I am just looking for a more streamlined solution – ArcticMediaRyan Mar 31 '22 at 22:47

1 Answers1

2

For the weights, you can use the Array.fill function as seen in this answer.

// 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)

For the heights, you can use a simple algorithm as a while loop with a condition for the increment

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)
RenaudC5
  • 3,553
  • 1
  • 11
  • 29
  • THANK YOU - The height is similar to mine, ill clean up your code in a few here to account for ' and " in the array. The weight one was the one I was having an issue with and yours is a LOT smaller than mine is and easy enough to add lb to. THANK YOU – ArcticMediaRyan Mar 31 '22 at 22:50
  • 1
    With some edits as i did, you can do it in one line of code. But do not forget that sometimes, bigger code are easier to maintain and read ;) – RenaudC5 Mar 31 '22 at 22:52
  • 1
    Completely agree, the one thing I was struggling with was my logic in my head. I agree your "non one line" is more readable and the one I will work with, however, when you look at the eight, I started to look at that and think "there has to be a cleaner way" - so knowing you came up with the same solution means I am not losing my mind - thank you! – ArcticMediaRyan Mar 31 '22 at 22:55