JSON –
"offers": [
{
"loanType": "432321CMP",
},
{
"loanType": "788407CMP",
},
{
"loanType": "97934OOW",
},
{
"loanType": "8293EWC-AS",
},
{
"loanType": "6653EWC-AS",
}
]
I want to filter data basis the substring in the end ["EWC-AS", "CMP"]
myArrayFiltered = offers.filter(function(v) {
return (["EWC-AS"].indexOf(v.loanType)) > -1;
});
if I pass on the exact value to match it works, but for subtring it does not.
Thanks in Advance for the help
Array.prototype.indexOf()
looks for exact matches, not substrings. Usefind()
to search an array using a callback function so you can specify how to match it. And useString.prototype.includes()
to test for a substring match.You can filter it by using filter in combination with find and endsWith to check if the substring is at the end of the string.