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

257
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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