admin管理员组

文章数量:1305164

So I wanted to choose an array index based off its percentage in the array.

The "percentage in the array" is just a function of the index, so like for an 11-element array the 50% index would be 5.

const numbers = [0 , 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];

I imagine I'd want to first go ahead by grabbing the length of the array to have a solid number to work a percentage from.

numbers.length; // 14

Though how would I then go about using a percentage to go into the array using the length to select an index that NEAREST matches the percentage? For example if I wanted to select the index that was nearest to being on 25% of the array?

So I wanted to choose an array index based off its percentage in the array.

The "percentage in the array" is just a function of the index, so like for an 11-element array the 50% index would be 5.

const numbers = [0 , 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];

I imagine I'd want to first go ahead by grabbing the length of the array to have a solid number to work a percentage from.

numbers.length; // 14

Though how would I then go about using a percentage to go into the array using the length to select an index that NEAREST matches the percentage? For example if I wanted to select the index that was nearest to being on 25% of the array?

Share Improve this question edited Jul 20, 2021 at 15:40 Harry asked Jul 20, 2021 at 15:20 HarryHarry 3806 silver badges19 bronze badges 6
  • You need to decide what "percentage" means to you. You need to go through the array and calculate this "percentage" for each value in the array. Then you need to decide what "nearest" means. Then you need to find the percentage that is "nearest" to the given one and get its index. – Heretic Monkey Commented Jul 20, 2021 at 15:26
  • 1 @HereticMonkey I could be wrong but I think the "percentage in the array" is just a function of the index, so like for an 11-element array the 50% index would be 5. – Pointy Commented Jul 20, 2021 at 15:33
  • @Pointy That's certainly one interpretation of that phrase. I tend to make as few assumptions about other people's questions as possible. – Heretic Monkey Commented Jul 20, 2021 at 15:36
  • Yeah that's exactly it @Pointy , you explained it a lot better thank you :D – Harry Commented Jul 20, 2021 at 15:36
  • @Harry You can and should edit your question to clarify it. – Heretic Monkey Commented Jul 20, 2021 at 15:37
 |  Show 1 more ment

3 Answers 3

Reset to default 8

I think you are looking for something like this.

const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];

const percentage = 50

let indexAtPercentage = Math.round((numbers.length - 1) * percentage / 100)

console.log("index: " + indexAtPercentage + "\nnumber at index: " + numbers[indexAtPercentage])

You can calculate the index or value of a given percentage by subtracting one from the length of the array, multiplying it by the percentage, and finally flooring the result.

The percentage parameter should be a value between 0.00 (0%) and 1.00 (100%).

const
  indexOfPercent = (arr, percent) => Math.floor((arr.length - 1) * percent),
  valueOfPercent = (arr, percent) => arr[indexOfPercent(arr, percent)];

const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];

for (let i = 0; i < numbers.length; i++) {
  const
    percent = i / (numbers.length - 1),
    index = indexOfPercent(numbers, percent),
    value = valueOfPercent(numbers, percent);

  console.log(`${i} ${percent.toFixed(2)} ${index} ${value}`);
}
.as-console-wrapper { top: 0; max-height: 100% !important; }

I believe it is as clear as calculating the required index by getting the desired percentage mark from the length.

const numbers = [0 , 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];

let desiredPercentage = 25;

if (desiredPercentage) {

  let indexAtPercent = Math.floor(numbers.length * desiredPercentage / 100);
  
  if (indexAtPercent)
    indexAtPercent--;
    
  console.log(numbers[indexAtPercent]);
}

本文标签: javascriptChoose array index by percentageStack Overflow