user592638
user592638

Reputation:

Loop array of objects to return correct outcome

I have an array with object:

 next: [
        {
          max_score: 5,
          outcome: "rest_and_come_back_later"
        },
        {
          max_score: 49,
          outcome: "see_a_doctor"
        },
        {
          outcome: "go_to_emergency_room"
        }
      ]

And a variable that holds a patientScore, let us say that the patientScore is 70. If the score is smaller then 5 it should return the outcome rest_and_come_back_later and if it is then max_score 49 it should return the right outcome. If it higher then 49 it should return the outcome : go_to_emergency_room.

What is the best way to do this in javascript?

Does simple ifelse do the job?, like this:

next.forEach((item) => {
    if(patientScore < item.max_score && patientScore >= item.max_score){
        return console.log("max_score: " + item.max_score)
    }else if(patientScore > item.max_score){ return console.log("max_score: " + item.max_score)}})

Upvotes: 1

Views: 64

Answers (3)

Jonathan
Jonathan

Reputation: 9151

The easiest way to do this is to define your array with scores in the correct order and then use Array.prototype.find to return score <= item.max_score

const list = [
  {
    max_score: 5, outcome: "rest_and_come_back_later"
  },
  {
    max_score: 49, outcome: "see_a_doctor"
  },
  {
    max_score: Infinity, outcome: "go_to_emergency_room"
  }
];

function test(score) {
  // Here it is:
  const item = list.find(i => score <= i.max_score);
  console.log(item.outcome);
}

const testScores = [1, 5, 12, 50, 100];
testScores.forEach(test);

Upvotes: 0

Ele
Ele

Reputation: 33726

  1. You're returning a value undefined return console.log(...) and not only that, but also inside of the handler you're using for the function Array.prototype.forEach which it doesn't make sense.
  2. An alternative is sorting the array and the just make <= comparison in order to find the object with the right max_score.

let next = [{      max_score: 5,      outcome: "rest_and_come_back_later"    },    {      max_score: 49,      outcome: "see_a_doctor"    },    {      outcome: "go_to_emergency_room"    }  ],
    // Sort the array to avoid multiple OR conditions.
    array = next.slice().sort((a, b) => {
      if (!('max_score' in a)) return Number.MAX_SAFE_INTEGER;
      if (!('max_score' in b)) return Number.MIN_SAFE_INTEGER;  
      return a.max_score - b.score;
    }),
    // This function finds the specific 'outcome' just comparing the 
    // current index.
    findDesc = (arr, score) => {
      for (let i = 0; i < arr.length; i++) {
        if (score <= arr[i].max_score) return arr[i].outcome;
      }
      return arr.slice(-1).pop().outcome;
    }

console.log(findDesc(array, 4));
console.log(findDesc(array, 5));
console.log(findDesc(array, 48));
console.log(findDesc(array, 49));
console.log(findDesc(array, 50));
console.log(findDesc(array, 70));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

Dacre Denny
Dacre Denny

Reputation: 30360

If I understand you question correctly, then one approach to this problem might be to define a function getOutcome() as shown below.

This method returns the desired outcome, based on input patientScore parameter that you pass it:

var object = {
  next : [
    {
      max_score: 5,
      outcome: "rest_and_come_back_later"
    },
    {
      max_score: 49,
      outcome: "see_a_doctor"
    },
    {
      outcome: "go_to_emergency_room"
    }
  ]
};


function getOutcome (score) {

  return object.next.filter(item => {

    if(score < 5) {
      return (item.max_score <= 5)
    }
    else if(score > 49) {
      return (item.max_score >= 49)
    }
    else {
      return (item.max_score > 5 && item.max_score < 49) || (item.max_score === undefined)
    }

  }).map(item => item.outcome)[0]
}

console.log('patientScore = 70', getOutcome(70) );
console.log('patientScore = 3', getOutcome(3) );
console.log('patientScore = 25', getOutcome(25) );

Upvotes: 0

Related Questions