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

235
Vistas
How to check if key exists in a list of key value array

I have a like that is like

[
   {Key: 'FName', Value: 'John'},
   {Key: 'LName', Value: 'Doe'},
   {Key: 'Age', Value: '30'},
   {Key: 'Person', Value: 'true'}
]

How is it possible to check if Person exists as a Key?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Solution 1

You can use <Array>.some

I also made the function take the key you are searching in and the value that you are seeking

const arr = [
   {Key: 'FName', Value: 'John'},
   {Key: 'LName', Value: 'Doe'},
   {Key: 'Age', Value: '30'},
   {Key: 'Person', Value: 'true'}
];




function isValueInsideField(arr, fieldName, value){
  return arr.some(e => e[fieldName] === value);
}

console.log(isValueInsideField(arr, 'Key', 'Person'))
console.log(isValueInsideField(arr, 'Key', '123'))

Solution 2

if you want to retrieve another value from the array

const arr = [
   {Key: 'FName', Value: 'John'},
   {Key: 'LName', Value: 'Doe', someKey:false},
   {Key: 'Age', Value: '30'},
   {Key: 'Person', Value: 'true'}
];




function retriveValueIfKeyExists(arr, keyFieldName, value, valueFieldName){
  // nullish concealing operator to return value even if it is falsy
  // you can change it with || operator  but this might lead to un wanted results
  // if the value is 0 or the boolean value false
  return arr.filter(e => e[keyFieldName] === value)[0]?.[valueFieldName] ?? null;
}

console.log(retriveValueIfKeyExists(arr, 'Key', 'Person', 'Value'))
console.log(retriveValueIfKeyExists(arr, 'Key', 'LName', 'someKey'))

console.log(retriveValueIfKeyExists(arr, 'Key', '123'))

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here is one way:

const vals = [
   {Key: 'FName', Value: 'John'},
   {Key: 'LName', Value: 'Doe'},
   {Key: 'Age', Value: '30'},
   {Key: 'Person', Value: 'true'}
   ];

const personExists = () => vals.filter(x => x.Key === 'Person').length > 0;
console.log(personExists());
about 4 years ago · Juan Pablo Isaza Denunciar

0

There can be multiple ways to do that: Third one will return the value as well

const list = [
    {Key: 'FName', Value: 'John'},
    {Key: 'LName', Value: 'Doe'},
    {Key: 'Age', Value: '30'},
    {Key: 'Person', Value: 'true'}
];

function checkKeyClassical(key, list) {
    for (let i = 0; i < list.length; i++) {
        if (list[i].Key === key) {
            return true;
        }
    }
    return false;
}

function checkKeyOnlyExistence(key, list) {
    return list.some(item => item.Key === key);
}

function checkKeyWithValue(key, list) {
    let result = list.find(item => item.Key === key);
    return result ? result.Value : false;
}

console.log(checkKeyClassical('FName', list)); // true
console.log(checkKeyOnlyExistence('FName', list)); // true
console.log(checkKeyWithValue('FName', list)); // John

console.log(checkKeyClassical('NOT_EXISTS', list)); // false
console.log(checkKeyOnlyExistence('NOT_EXISTS', list)); // false
console.log(checkKeyWithValue('NOT_EXISTS', list)); // false
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