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

263
Views
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 answers
Answer question

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 Report

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