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

129
Visualizações
Running specific check on the data key element

I have the following code working fine which checks for null and if a null is encountered, it stops checking and prints a console log.

const data = [
  {
    "filename": "deletetestOne.js",
    "answer1": "Y",
    "answer2": null,
    "riskLevel1": null,
    "description": ""
  },
  {
    "filename": "deletetestThird.js",
    "answer1": null,
    "answer2": "Y",
    "riskLevel1": "1",
    "description": ""
  }
]

const isNull = (val) => val === null;
const hasNull = data.some(function (item) {
    return Object.values(item).some(isNull);
});
console.log(hasNull ? "Some of the values are null": "All good");

The above code has been just included to show how I'm checking for null values which is not related to what I'm looking for. My new data looks as shown below without any null values:

Now I have a slightly different data which doesn't contain null values any more:

const data = [
  {
    "filename": "deletetestOne.js",
    "answer1": "Y",
    "answer2": "N",
    "riskLevel1": "2",
    "description": ""
  },
  {
    "filename": "deletetestThird.js",
    "answer1": "N",
    "answer2": "N",
    "riskLevel1": "1",
    "description": ""
  }
]

And I want to run a specific check on the above data on answer1 key element of the data. If it finds Y then it should stop checking else keep on printing values of answer1, which is probably going to be N.

My Requirement:

I want to check if answer1 key element contains Y and if it does, then I want to break out of the loop and just print a console log saying Found Y in answer1. If it doesn't, then I can just print a console log saying Didn't find Y in answer1

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

0

Simply use .some() and check for your "answer1" property value being "Y"

const data1 = [{"answer1": "Y"}, {"answer1": "N"}];
const data2 = [{"answer1": "N"}, {"answer1": "N"}];

const someIsY = (arr) => arr.some(ob => ob.answer1 === "Y");

console.log(someIsY(data1)) // true
console.log(someIsY(data2)) // false

to get your specific "log" use:

const report = someIsY(data1) ? "Found Y in answer1" : "Didn't find Y in answer1";
console.log(report);  // "Found Y in answer1"

to make your function more flexible in matching use:

const data1 = [{"answer1": "Y"}, {"answer1": "N"}];
const data2 = [{"answer1": "N"}, {"answer1": "N"}];

const somePropVal = (arr, prop, val) => arr.some(ob => ob[prop] === val);

console.log(somePropVal(data1, "answer1", "Y")) // true
console.log(somePropVal(data2, "answer1", "Y")) // false
console.log(somePropVal(data2, "answer1", "N")) // true

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