I have an object with keys and objects which I am trying to see if I can filter out and return only the ones that match the criteria. This is the object.
the random number keys will or could be different.
{
"0e93e707-e5aa-4d2b-91ab-a8dec97af344": {
"Id": "0e93e707-e5aa-4d2b-91ab-a8dec97af344",
"ExternalSecurityListId": null,
"CreatorId": "557fa8b2-59db-430d-9595-cd17884c9151",
"Name": "Chris",
"PartnerName": null,
"Type": "Watch",
"CostBasis": 0,
"Processing": false,
"NotificationSettings": {
"SourceId": "0e93e707-e5aa-4d2b-91ab-a8dec97af344",
"SourceType": "SecurityList",
"ChannelTypes": null,
"NotificationFilters": []
},
"Owner": null,
"SharingStatus": "None",
"CreatedDate": "2023-02-01T20:31:21.848Z",
"ModifiedDate": "2023-02-10T17:12:21.053Z",
"IsGroup": false,
"UnderlyingSecurityListIds": [],
"MaturingSoon": false
},
"ed89d5a3-464b-4bd7-b9bb-e42ee87f6520": {
"Id": "ed89d5a3-464b-4bd7-b9bb-e42ee87f6520",
"ExternalSecurityListId": null,
"CreatorId": "557fa8b2-59db-430d-9595-cd17884c9151",
"Name": "Chris Portfolio Two",
"PartnerName": null,
"Type": "Portfolio",
"CostBasis": 100000,
"Processing": false,
"NotificationSettings": {
"SourceId": "ed89d5a3-464b-4bd7-b9bb-e42ee87f6520",
"SourceType": "SecurityList",
"ChannelTypes": null,
"NotificationFilters": []
},
"Owner": null,
"SharingStatus": "None",
"CreatedDate": "2023-03-20T19:20:02.16Z",
"ModifiedDate": "2023-03-20T19:20:17.117Z",
"IsGroup": false,
"UnderlyingSecurityListIds": [],
"MaturingSoon": false
},
}
I tried filterting by key such as this but its not returnig any results.
const allowed = ['Portfolio'];
const filtered = Object.keys(portfoliosData)
.filter(key => allowed.includes(key))
.reduce((obj, key) => {
obj[key] = portfoliosData[key];
return obj;
}, {});
const filtered = Object.keys(portfoliosData)
.filter(key => allowed.includes(key))
.reduce((obj, key) => {
obj[key] = portfoliosData[key];
return obj;
}, {});
any thoughts or suggestions?
>Solution :
Ok, based on your comments, this is an option to accomplish that:
Object.values(portfoliosData).filter(({Type}) => Type == "Portfolio")
It will list only the values of the topmost object (instead of the keys), and then you iterate on filter with a lambda that captures the property Type and checks if it is equals to the expected value