SDK
SDK

Reputation: 1518

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

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 in 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 hard coding. I need to make the if condition dynamic.

i have done something like this to make as dynamic:

export const GeneralFunction = (data) => {
    let array = data.map(i => i?.latestMetric?.numberOfProjects || 0)
    let min = Math.min.apply(null, array);
    let Maximum = Math.max(...array)
    let Minimum = Math.min.apply(null, array.filter(n => n !== min));
    let range = (Maximum - Minimum) / 4
    let FirstRange = [Minimum, Minimum + range]
    let SecondRange = [FirstRange[1], FirstRange[1] + range]
    let ThirdRange = [SecondRange[1], SecondRange[1] + range]
    let FourthRange = [ThirdRange[1], ThirdRange[1] + range]

    let Obj = {
        firstRange: FirstRange,
        secondRange: SecondRange,
        thirdRange: ThirdRange,
        fourthRange: FourthRange
    }
    return Obj;

}

Is this can be refactored?

Upvotes: 1

Views: 574

Answers (1)

Vivek Doshi
Vivek Doshi

Reputation: 58573

Logical Solution :

1) find min and max from your array , lets say 
    min = 2000    
    max = 4000

2) now we have to create 4 range
    (max-min)/no of range
    (4000 - 2000)/4 
    = 500

3) create your range
    First range = 2000 to 2000 + 500
    Second range = 2000+500 to 2000+500+500
    ...

4) then loop through your array and compare in which range it belongs 

WORKING DEMO (Before copy paste try to solve it by your self)

Upvotes: 2

Related Questions