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

104
Visualizações
Verify only single value of one property in an array and matches criteria

I want to return true if all the ids that are not null are of the same values and where flag is true. Values where flag is true and id is null are valid.

Valid list. All flags and true and all the values of the ids that are not null are of the same value ("00001")

const arr = [
   {
      id = '00001',
      flag: true
   },
   {
      id = "00001",
      flag: true
   },
   {
      id = "",
      flag: true
   }
];

Invalid: ids that are not null do not have the same value.

const arr = [
   {
      id = '00001',
      flag: true
   },
   {
      id = "00002",
      flag: true
   },
   {
      id = "",
      flag: true
   }
];

Invalid: ids are of the same value, but one flag is false.

const arr = [
   {
      id = '00001',
      flag: true
   },
   {
      id = "00001",
      flag: false
   },
   {
      id = "",
      flag: true
   }
];

Below works, but looking for a more succinct way if possible.

function meetsCriteria(arr) {
    const ids = new Set();
    let isValid = true;
    for (const a of arr) {
       if (a.id) {
           ids.add(a.id);
       }
       if (ids.size > 1 || !a.flag) {
         isValid = false;
       }
       if (!isValid) {
           break;
       }
    }
    return isValid;
}

Thanks.

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

0

You can use Array#every() and compare against the properties of the first element of the array.

const validate = (arr) =>
  arr.every((a, _, [b]) => a.flag && (a.id === b.id || a.id === ''));

const arr1 = [{ id: '00001', flag: true }, { id: '00001', flag: true }, { id: '', flag: true },];
const arr2 = [{ id: '00001', flag: true, }, { id: '00002', flag: true, }, { id: '', flag: true, },];
const arr3 = [{ id: '00001', flag: true, }, { id: '00001', flag: false, }, { id: '', flag: true, },];

console.log(validate(arr1)); // true
console.log(validate(arr2)); // false
console.log(validate(arr3)); // false

about 4 years ago · Juan Pablo Isaza Relatório

0

const arr = [{ id: '00001', flag: true }, { id: '00001', flag: true }, { id: '', flag: true, },];

/**
 * @param {{ id: string, flag: boolean }[]} arr The array to check
 */
function meetsCriteria(arr) {
    let id = "";
    return !arr.some( o => {
        if (!o.flag) return true;
        if (id === "") {
            id = o.id;
        } else if (id !== o.id && o.id !== "") {
            return true;
        }
        return false;
    });
}

console.log( meetsCriteria(arr) ); 

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