Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

filter map objects using javascript #621

Answered by mdazfar2
NitishKumar525 asked this question in Q&A
Discussion options

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

 [
 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

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();
You must be logged in to vote

@NitishKumar525 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;
})

); };

Replies: 3 comments

Comment options

@NitishKumar525 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;
})

); };

You must be logged in to vote
0 replies
Answer selected by NitishKumar525
Comment options

thank you @mdazfar2 - understood what i was doing wrong. thanks for explanation. can you confirm if your final code is properly formatted in the answer.

You must be logged in to vote
0 replies
Comment options

thanks

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /