I have a JavaScript object structured like this:
{
"3": {
"id": 3,
"first": "Lisa",
"last": "Morgan",
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbd7d6d4c9dcdad5fbdcd6dad2d795d8d4d6">[email protected]</a>",
"phone": "(508) 233-8908",
"status": 0
},
"4": {
"id": 4,
"first": "Dave",
"last": "Hart",
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="14707c756660547379757d783a777b79">[email protected]</a>",
"phone": "(509) 874-9411",
"status": 1
}
}
I am interested in filtering the object to extract records with a status value of '1'. One way I can achieve this is by using array filter as shown in the code snippet below:
var filterJSON = Object.values(obj).filter(function (entry) {
switch(frmFilter){
case '1':
return entry.status === 1;
break;
case '2':
return entry.status === 0;
break;
default:
return entry;
}
});
The issue with this approach is that it converts the filtered data into an array. I want to maintain the structure of my data as an object, similar to the original format before applying the filter. Is there a method to filter through the object while preserving its object type and achieving the same output as filtering through an array?