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: '[email protected]', value: 10, name: 'ram'},
{id:2, email: '[email protected]', value: 20, name: 'Tom'},
{id:3, email: '[email protected]', value: 30, name: 'Lucas'},
{id:4, email: '[email protected]', value: 40, name: 'Chris'},
{id:5, email: '[email protected]', 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('[email protected]');
scenario 1:
[
{id:1, email: '[email protected]', value: 10, name: 'ram'},
{id:2, email: '[email protected]', value: 20, name: 'Tom'}
]
scenario 2:
getList('Chris');
[
{id:4, email: '[email protected]', value: 40, name: 'Chris'}
]
CodePudding user response:
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: '[email protected]', value: 10, name: 'ram' },
{ id: 2, email: '[email protected]', value: 20, name: 'Tom' },
{ id: 3, email: '[email protected]', value: 30, name: 'Lucas' },
{ id: 4, email: '[email protected]', value: 40, name: 'Chris' },
{ id: 5, email: '[email protected]', 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('[email protected]'));
console.log(getList('Chris'));
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
Simplified version
var objarr = [
{ id: 1, email: '[email protected]', value: 10, name: 'ram' },
{ id: 2, email: '[email protected]', value: 20, name: 'Tom' },
{ id: 3, email: '[email protected]', value: 30, name: 'Lucas' },
{ id: 4, email: '[email protected]', value: 40, name: 'Chris' },
{ id: 5, email: '[email protected]', value: 30, name: 'Lucas Tim' }
]
const getList = (val) => objarr.filter(e => Object.values(e).includes(val))
console.log(getList('[email protected]'));
console.log(getList('Chris'));
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
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: "[email protected]", value: 10, name: "ram" },
{ id: 2, email: "[email protected]", value: 20, name: "Tom" },
{ id: 3, email: "[email protected]", value: 30, name: "Lucas" },
{ id: 4, email: "[email protected]", value: 40, name: "Chris" },
{ id: 5, email: "[email protected]", value: 30, name: "Lucas Tim" },
];
const getList = val => objarr.filter((o) => Object.values(o).some(v => v === val));
console.log(getList("[email protected]"));
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; }
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>