Home > Blockchain >  Sorting keys from and object based on the difference in value Javascript?
Sorting keys from and object based on the difference in value Javascript?

Time:05-13

let scores = {
  A: 30,
  B: 40,
  C: 35,
  D: 90
}



function getScores(students) {

  let result = []
  let keys = Object.keys(scores)
  let difference = 0
  for (let i = 0; i < keys.length; i  ) {
    console.log('key', keys[i])
    if (scores[keys[i]] < scores[keys[i   1]]) {
      difference = scores[keys[i   1]] - scores[keys[i]]
      if (difference <= 15) {
        result.push(keys[i])
        result.push(keys[i   1])
        console.log('result', result)
      }
      console.log('>>>', difference)
    } else {
      difference = scores[keys[i]] - scores[keys[i   1]]
      if (difference <= 15) {
        if (!result.includes(keys[i])) {
          result.push(keys[i])
        }
        result.push(keys[i   1])
        console.log('result', result)
      }

    }
    if (!result.includes(keys[i])) {
      result.push([keys[i]])
    }
    console.log('<<<', difference)
  }

}

getScores(scores)

I'm trying to group these into an array based on a condition that the value is less than 15. If a key and the next key have a difference less than 15 then they should be pushed to the results array. This works some what in the code I have. When run I get result = ['A', 'B', 'C'] What I'm trying to get is result = [['A', 'B', 'C'], ['D']] so A B and C are 15 or less so they are in one nested array and D is in a separate array. I realize this is probably less than optimized code. Looking for any suggestions. Thanks

CodePudding user response:

You should also take care of negative values so use Math.abs.

let scores = { A: 30, B: 40, C: 35, D: 90 };

const result = [];

Object.entries(scores).reduce(
  ([previousKey, previousValue], [key, value]) => {
    if (result.length === 0)
      result.push([previousKey]);
    if (Math.abs(previousValue - value) <= 15)
      result[result.length - 1].push(key);
    else
      result.push([key]);
    return [key, value];
  }
);

console.log(result);

CodePudding user response:

Sounds like a job for reduce.

You can store the object keys in a variable, then reduce over the array. In the reducer function, check whether the value of the property after the current has a score difference of less than 16, and if so, push the current item to the last array in the accumulator array. Otherwise, push a new array with only the current item.

let scores = {A: 30, B: 40, C: 35, D: 90};

const keys = Object.keys(scores)
const result = keys.reduce((a, b, i) => {
  if (scores[b] - scores[keys[i   1]] <= 15 && a[a.length - 1]) {
    a[a.length - 1].push(b)
  } else {
    a.push([b])
  }
  return a;
}, [])

console.log(result)

  • Related