Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

filter map objects using javascript

I have a map object with key value pair as shown below

const input = new Map([
    [
      0,
      {
        testErrorSummary: "ServiceC\nV4/\n\tcom.dmv.smp",
        appErrorSummary: '{"handler":null,"sourcetype":null}',
        invocations: [
          { radar: "", testCaseMilestone: "1G05" },
          { radar: "3450", testCaseMilestone: "1G15" },
        ],
      },
    ],
    [
      1,
      {
        testErrorSummary: 'java: \nEx:\n <"()">\n <" "code": "403"">',
        appErrorSummary: '{"handler":null,"sourcetype":null}',
        invocations: [
          { radar: "1532", testCaseMilestone: "1G12" },
          { radar: "1954", testCaseMilestone: "1G24" },
        ],
      },
    ],
    [
      2,
      {
        testErrorSummary: "com.dmv: Timeout while waiting.",
        appErrorSummaryKeyword: 'Cannot invoke "com.dmv.getEcid()"',
        hasRadarHistory: false,
        invocations: [
          { radar: "3444", testCaseMilestone: "1G45" },
          { radar: "", testCaseMilestone: "1G90" },
        ],
      },
    ],
  ]);

I have a search input where i enter the string. Based on this string value, i want to filter this above map object. Suppose user enters "50". So i need to check for properties testErrorSummary, appErrorSummary and radar and return only those mapped objects which matches with user entries.

So in this case, user enters 50, i am expecting the below result because those get matched with radar having 50 in it. So filterFailureInput = 50

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Expected output

new Map([
    [
      0,
      {
        testErrorSummary: "ServiceC\nV4/\n\tcom.dmv.smp",
        appErrorSummary: '{"handler":null,"sourcetype":null}',
        invocations: [
          { radar: "", testCaseMilestone: "1G05" },
          { radar: "3450", testCaseMilestone: "1G15" },
        ],
      },
    ],
  ]);

In order to achieve this way of filtering, i am doing the following but i dont get the expected filter results. Can someone let me know where i am going wrong with this.

const searchException = () => {
        return new Map(
          Array.from(input).filter(([key, value]) => {
            const matchInvocations = value.invocations.some(
              (invocation) => {
                (invocation?.radar &&
                  invocation?.radar.includes(filterFailureInput)) ||
                (value?.testErrorSummary?.toLowerCase()
                    .includes(filterFailureInput.toLowerCase())) ||
                (value?.appErrorSummaryKeyword?.toLowerCase()
                    .includes(filterFailureInput.toLowerCase()))
          });
            return matchInvocations;
          })
        );
      };

const expectedOutput = searchException();

>Solution :

Your filtering function looks mostly correct, but there’re some issues.

  1. you should add the missing return some() callback.
  2. you should add a null-safe check for appErrorSummaryKeyword.

Here’s updated code.

const searchException = () => {
  return new Map(
    Array.from(input).filter(([key, value]) => {
      const matchInvocations = value.invocations.some((invocation) => 
        invocation?.radar?.includes(filterFailureInput)
      );

      const matchErrorSummary =
        value?.testErrorSummary?.toLowerCase().includes(filterFailureInput.toLowerCase()) ||
        value?.appErrorSummaryKeyword?.toLowerCase()?.includes(filterFailureInput.toLowerCase());

      return matchInvocations || matchErrorSummary;
    })
  );
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading