Venkatesh Prasad
Venkatesh Prasad

Reputation: 91

Find the best match in an array

I have got the following array:

let x = [
  { name: "Bad", value: 2 },
  { name: "Critical", value: 1 },
  { name: "High", value: 5 },
  { name: "Medium", value: 5 },
];

The expectation is to look for "Critical" first, if the array has it, return that, else look for "High" then look for "Medium" and so on.

Upvotes: 1

Views: 231

Answers (3)

Som Shekhar Mukherjee
Som Shekhar Mukherjee

Reputation: 8168

You can store the priorities in an array and then loop over the array and for every priority check if there's an object and whenever an object is found, return it. If the entire priorities array is exhausted then return null (or whatever you want).

const arr = [
    { name: "Bad", value: 2 },
    { name: "Critical", value: 1 },
    { name: "High", value: 5 },
    { name: "Medium", value: 5 },
  ],
  priorities = ["Critical", "High", "Medium", "Bad"],
  search = (arr, priorities) => {
    for (let p of priorities) {
      const obj = arr.find(({ name }) => name === p);
      if (obj) {
        return obj;
      }
    }
    return null;
  };

console.log(search(arr, priorities));

You can also sort the array based on the priority.

  • Create a Map that stores the priorities.
  • Sort arr based on the priorities stored in the map.

const arr = [
    { name: "Bad", value: 2 },
    { name: "Critical", value: 1 },
    { name: "High", value: 5 },
    { name: "Medium", value: 5 },
  ],
  priorities = new Map([
    ["Critical", 4],
    ["High", 3],
    ["Medium", 2],
    ["Bad", 1],
  ]),
  sortedArr = [...arr].sort(
    (a, b) => priorities.get(b.name) - priorities.get(a.name)
  );

console.log(sortedArr);

Upvotes: 3

PeterKA
PeterKA

Reputation: 24638

First, define the order in which you want to look for items, then use that to sort the items in the array. Lastly, find based on all the items in your sort order. The first match will be returned, since the items are sorted.

const x = [{name: 'Bad',value: 2}, {name: 'High', value: 5}, {name: 'Medium', value: 5}, {name: 'Critical', value: 1}],
      order = ['Critical','High','Medium'],
      //sort according to above order
      output = x.sort(
          ({name:na},{name:nb}) => 
          order.findIndex(v => v === na) - order.findIndex(v => v === nb)
      )
      //Now find items matching name in order
      .find( ({name}) => order.includes(name) );
      
console.log( output );

NOTE

  • Since your data is already sorted in the desired order, I moved the element with name = "Critical" to the end of the array.
  • If the data will always be sorted according to the priority you want to find the items, then no sorting is needed.

Upvotes: 0

let value = x.find(x => x.name == "Critical") 
  || x.find(x => x.name == "High")
  || x.find(x => x.name == "Medium") ...

Upvotes: 0

Related Questions