Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

112
Visualizações
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.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

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);

about 4 years ago · Juan Pablo Isaza Relatório

0

This will iterate over the array once until it finds the highest value and break the loop once it finds that value. Maximum number of iterations is N where N is x.length;

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

function findVal(arr) {
  let returnVal = null;
  for (let i = 0; i < arr.length && returnVal === null; i++) {
    switch (arr[i].name) {
      case 'Critical':
        returnVal = arr[i];
        break;
      case 'High':
        returnVal = arr[i];
        break;
      case 'Medium':
        returnVal = arr[i];
        break;
      case 'Low':
        returnVal = arr[i];
        break;
      default:
        break;
    }
  }
  return returnVal;
}

findVal(x);

Note: It would be far simpler to designate a numerical value to the array values so that you could determine the severity simply by using the highest/lowest value rather than string matching

about 4 years ago · Juan Pablo Isaza Relatório

0

let value = x.find(x => x.name == "Critical") 
  || x.find(x => x.name == "High")
  || x.find(x => x.name == "Medium") ...
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda