Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

136
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda