Reputation: 4599
I have a single dimensional array with the below format,
let e = ["CGST:20", "SGST:20", "IGST:20", "CESS:20", "GSTIncentive:20", "GSTPCT:20"].map(i=>i.trim());
And want to match the specific string in the array, say for example, match the part of the string "IGST" in the "IGST:20".
I have tried in the below way, but it always matching the first key in the array,
if(/^IGST:/.test(e)){
console.log("matched")
} else {
console.log("Not matched")
}
Upvotes: 0
Views: 26
Reputation: 1074335
If your goal is to find out if that regular expression matches any entry in the array, you can use the some
function for that:
if (e.some(entry => /^IGST:/.test(entry)) {
console.log("matched")
} else {
console.log("Not matched")
}
If you want to find the matching entry, use find
instead. If you want its index, use findIndex
.
Details on MDN.
Upvotes: 1