filter map objects using javascript #621
|
I have a map object with key value pair as shown below 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 So in this case, user enters 50, i am expecting the below result because those get matched with Expected output- 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. |
Replies: 4 comments 1 reply
|
@NitishKumar525 Your filtering function looks mostly correct, but there're some issues.
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;
})); }; |
|
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. |
|
thanks |
|
The main issue is that your You can fix it like this: const searchException = () => {
return new Map(
Array.from(input).filter(([key, value]) => {
const searchValue = filterFailureInput.toLowerCase();
const matchInvocations = value.invocations?.some((invocation) =>
invocation?.radar?.toLowerCase().includes(searchValue)
);
const matchTestErrorSummary =
value?.testErrorSummary?.toLowerCase().includes(searchValue);
const matchAppErrorSummary =
value?.appErrorSummary?.toLowerCase().includes(searchValue);
const matchAppErrorSummaryKeyword =
value?.appErrorSummaryKeyword?.toLowerCase().includes(searchValue);
return (
matchInvocations ||
matchTestErrorSummary ||
matchAppErrorSummary ||
matchAppErrorSummaryKeyword
);
})
);
};
const expectedOutput = searchException();For example, when Also, your original code checks |
@NitishKumar525 Your filtering function looks mostly correct, but there're some issues.
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; })); };