1

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?

3
  • Please elaborate - what should the expected output be from your provided data, and (if possible) how should it be calculated? Commented May 7, 2020 at 6:47
  • 2
    Well.. you need to write a logic/algorithm. The good question would be you first trying ouut something and show what is going wrong and not ask us for an entier solution Commented May 7, 2020 at 6:47
  • what about missing ranges? Commented May 7, 2020 at 6:50

1 Answer 1

2

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)

Sign up to request clarification or add additional context in comments.

3 Comments

loop (no of range) times and adding it to min value
Sorry bro, atleast this is what you have to create by yourself, I can do it but not today it will also take 15 - 20 mins for me to code and it doesn't make any sense, and you have been trying for 1 or 2 hours only, try bit harder. you surely solve it :)
Pls see my edit..in the question..is that can be refactored?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.