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

256
Vistas
How to get the object array based on value of object in javascript

I would like to know how to get the object array list based on values matching in javascript

If any of object value matches with parameter, then return that array in javascript.

var objarr = [
  {id:1, email: 'xyz@gmail.com', value: 10, name: 'ram'},
  {id:2, email: 'xyz@gmail.com', value: 20, name: 'Tom'},
  {id:3, email: 'ss@gmail.com', value: 30, name: 'Lucas'},
  {id:4, email: 'ct@gmail.com', value: 40, name: 'Chris'},
  {id:5, email: 'tam@gmail.com', value: 30, name: 'Lucas Tim'}
]

function getList(val){
  var result=[];
  var checkdata = objarr.filter(e=>{
   if(Object.values(e)===val){
    result.push(e);
  }
return result;
})
}
console.log(result);

Expected Output:
getList('xyz@gmail.com');
scenario 1: 
[
  {id:1, email: 'xyz@gmail.com', value: 10, name: 'ram'},
  {id:2, email: 'xyz@gmail.com', value: 20, name: 'Tom'}
]
scenario 2:
getList('Chris');
[
  {id:4, email: 'ct@gmail.com', value: 40, name: 'Chris'}
]
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Your Array.filter function should return either true or false depending on the search criteria.

Object.values returns an Array as output. To check whether a value is in an array, you can use Array.includes.

You should chck for value with Object.values(e).includes(val)

Working Fiddle

var objarr = [
  { id: 1, email: 'xyz@gmail.com', value: 10, name: 'ram' },
  { id: 2, email: 'xyz@gmail.com', value: 20, name: 'Tom' },
  { id: 3, email: 'ss@gmail.com', value: 30, name: 'Lucas' },
  { id: 4, email: 'ct@gmail.com', value: 40, name: 'Chris' },
  { id: 5, email: 'tam@gmail.com', value: 30, name: 'Lucas Tim' }
]

function getList(val) {
  var checkdata = objarr.filter(e => {
    if (Object.values(e).includes(val)) {
      return true;
    }
    return false;
  })
  return checkdata;
}
console.log(getList('xyz@gmail.com'));
console.log(getList('Chris'));

Simplified version

var objarr = [
  { id: 1, email: 'xyz@gmail.com', value: 10, name: 'ram' },
  { id: 2, email: 'xyz@gmail.com', value: 20, name: 'Tom' },
  { id: 3, email: 'ss@gmail.com', value: 30, name: 'Lucas' },
  { id: 4, email: 'ct@gmail.com', value: 40, name: 'Chris' },
  { id: 5, email: 'tam@gmail.com', value: 30, name: 'Lucas Tim' }
]

const getList = (val) => objarr.filter(e => Object.values(e).includes(val))
console.log(getList('xyz@gmail.com'));
console.log(getList('Chris'));

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can make use of filter and using Object.values to get all the values of an object and then use some to get the desired result.

ONE LINER

objarr.filter((o) => Object.values(o).some((v) => v === val));

var objarr = [
  { id: 1, email: "xyz@gmail.com", value: 10, name: "ram" },
  { id: 2, email: "xyz@gmail.com", value: 20, name: "Tom" },
  { id: 3, email: "ss@gmail.com", value: 30, name: "Lucas" },
  { id: 4, email: "ct@gmail.com", value: 40, name: "Chris" },
  { id: 5, email: "tam@gmail.com", value: 30, name: "Lucas Tim" },
];

const getList = val => objarr.filter((o) => Object.values(o).some(v => v === val));

console.log(getList("xyz@gmail.com"));
console.log(getList("Chris"));
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

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