Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

234
Views
Cómo verificar si la clave existe en una lista de matriz de valores clave

tengo un como que es como

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

¿Cómo es posible verificar si Persona existe como Clave?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Solución 1

Puedes usar <Array>.some

También hice que la función tomara la clave que está buscando y el valor que está buscando.

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

Solución 2

si desea recuperar otro valor de la matriz

 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 Report

0

Aquí hay una manera:

 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 Report

0

Puede haber varias formas de hacerlo: la tercera también devolverá el valor

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!