skip to Main Content

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


  1. Assuming that your blob of JSON is being assigned to the portfoliosData variable, then Object.keys(portfoliosData) will return ["0e93e707-e5aa-4d2b-91ab-a8dec97af344", "ed89d5a3-464b-4bd7-b9bb-e42ee87f6520"]. Obviously the allowed array does not contain either of these keys, so your filter is (correctly) filtering out all options and returning you an empty array.

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
  3. This will filter the object dictionary into an array of [key, object]:

    const dictionary = {1: {Property: "correct"}, 2: {Property: "incorrect"}};
    const correctObjects = [];
    for (const key in dictionary) {
        const object = dictionary[key];
        if (object.Property === "correct") {
            correctObjects.push([key, object]);
        }
    }
    console.log(correctObjects);
    

    Outputs: [ ["1", {"Property":"correct"}] ].

    Login or Signup to reply.
  4. You could loop through Object.keys(portfoliosData) and then for each of them you would just check their portfolio type. Your allowed variable doesn’t seem to match how you want your app to work.

    keys = Object.keys(portfoliosData)
    filtered = []
    for (k of keys) {
        if (portfoliosData[k]['Type'] == 'Portfolio') {
            filtered.push(portfoliosData[k])
        }
    }
    
    Login or Signup to reply.
  5. Looks like your portfoliosData is a map where keys are ids and values are the corresponding data items that have different Types. You want to get a list of all items with Type in the allowed 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.

    Login or Signup to reply.
  6. I jus threw this together but see if this would work.

    const result = Object.values(data).filter((item) => item.Type === 'Portfolio')
    

    I put your example data into a const of data. So just change data to whatever your source is and this should filter it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search