I have an object with keys and objects. 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 filtering by key such as this but it’s not returning 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?
6
Answers
Assuming that your blob of JSON is being assigned to the
portfoliosData
variable, thenObject.keys(portfoliosData)
will return["0e93e707-e5aa-4d2b-91ab-a8dec97af344", "ed89d5a3-464b-4bd7-b9bb-e42ee87f6520"]
. Obviously theallowed
array does not contain either of these keys, so your filter is (correctly) filtering out all options and returning you an empty array.Ok, based on your comments, this is an option to accomplish that:
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
This will filter the object dictionary into an array of
[key, object]
:Outputs:
[ ["1", {"Property":"correct"}] ]
.You could loop through
Object.keys(portfoliosData)
and then for each of them you would just check their portfolio type. Yourallowed
variable doesn’t seem to match how you want your app to work.Looks like your
portfoliosData
is a map where keys are ids and values are the corresponding data items that have differentType
s. You want to get a list of all items withType
in theallowed
list.You can do something like this:
const allowedItems = Object.entries(portfolioData).filter(item => allowed.includes(item.Type))
This approach completely disregards the keys of the
portfolioData
since they are random.Object.values
returns the array of all items that you can filer over.I jus threw this together but see if this would work.
I put your example data into a const of data. So just change data to whatever your source is and this should filter it.