$0.12

How to assign dynamic range values in javascript based on given values?

Ask a question+
2

Here i am having an array of values

let array = [4023,4545,34,34353,34,454,4523,234,4555,232,4353,444,1232,4542,565,7,345,3456,8908]

I need to split out these array values into 4 dynamic range values.

For eg : 1 to 1000 , 1000 to 2000 , 3000 to 4000 , 4000 to 5000

But these range values has to be dynamic . Based on Highest and Lowest value from the array.

Please help me to find out this. Thanks you for advance

i have hard Coded values in the function updateColor [if conditions].

function updateColor() {
        function updateColorValue(colorJsonObject, colorValue) {
          const updatedProperties = { ...colorJsonObject.properties, color: colorValue };
          colorJsonObject.properties = updatedProperties;
        }
        colorData.features.map((colorObject) => mapData?.data?.map((apiData) => {
          if (colorObject.properties.name === apiData?.name) {
            if (
              apiData?.cumulativeMetric?.noOfProjects >= 1
              && apiData?.cumulativeMetric?.noOfProjects <= 1000
            ) {
              updateColorValue(colorObject, 5);
            } else if (
              apiData?.cumulativeMetric?.noOfProjects >= 1000
              && apiData?.cumulativeMetric?.noOfProjects <= 6000
            ) {
              updateColorValue(colorObject, 2);
            } else if (
              apiData?.cumulativeMetric?.noOfProjects >= 40000
              && apiData?.cumulativeMetric?.noOfProjects <= 50000
            ) {
              updateColorValue(colorObject, 3);
            }
          }
        }));
      }

instead of this coding. I need to make the if condition dynamic.

abdokb 40
add comment

1 Answer

0

I’m not exactly sure what you mean by dynamic, but this may help:

// Already defined:
let array = [4023,4545,34,34353,34,454,4523,234,4555,232,4353,444,1232,4542,565,7,345,3456,8908]

// First start with defining your ranges
const ranges = [
    [1,1000].
    [1000,2000],
    [3000,4000], // Do you not need 2000 to 3000?
    [4000,5000]
]
const splitArrays = []

// We can define this for helping us later
function inRange(x, min, max) {
    return ((x-min)*(x-max) <= 0);
}

function SplitArray() {
    splitArrays.push([array.filter((value) => (inRange(value,ranges[0][0],ranges[0][1]))])
    splitArrays.push([array.filter((value) => (inRange(value,ranges[1][0],ranges[1][1]))])
    splitArrays.push([array.filter((value) => (inRange(value,ranges[2][0],ranges[2][1]))])
    splitArrays.push([array.filter((value) => (inRange(value,ranges[3][0],ranges[3][1]))])
}

I have an idea for a “dynamic” ranged array splitting:

let array = [4023,4545,34,34353,34,454,4523,234,4555,232,4353,444,1232,4542,565,7,345,3456,8908]
let maxNum = array.reduce((a, b) => {return Math.max(a, b)})
let minNum = array.reduce((a, b) => {return Math.min(a, b)})
let numRanges = 4

let splitArray = []

function isBetween(x,min,max) {
    return ((x-min)*(x-max)<=0)
}

function SplitArrays() {
    splitArray = []
    splitArray.push(array.filter((value) => isBetween(value,minNum,minNum+(maxNum-minNum)/4)))
    splitArray.push(array.filter((value) => isBetween(value,minNum+(maxNum-minNum)/4,(maxNum+minNum)/2)))
    splitArray.push(array.filter((value) => isBetween(value,(maxNum+minNum)/2,maxNum-(maxNum-minNum)/4)))
    splitArray.push(array.filter((value) => isBetween(value,maxNum-(maxNum-minNum)/4,maxNum)))
}

The above code will split the array into 4 different arrays (depending on what value you put for your numRanges variable), but it doesn’t split it up very even depending on how spread appart your values are.

I hope this helps you with your problem.

Dark Vortex 0
add comment

Your Answer