javascript - Typescript array map vs filter vs? -


here's typescript method wants walk through array of strings, , return array of strings, where, strings match regexp (formatted "[la la la]" become "la la la" , strings don't match dropped. if input array is:

"[x]", "x", "[y]" 

it becomes

"x", "y" 

here's code:

questions(): string[] {     var regexp = /\[(.*)\]/;     return this.rawrecords[0].map((value) => {         console.log(value);         var match = regexp.exec(value);         if (match) {             return match[1];         }     }); } 

i end output this:

"x", undefined, "y" 

because of "if (match)". what's right typescript/javascript way of writing code?

just filter them out:

return this.rawrecords[0].map((value) => {         console.log(value);         var match = regexp.exec(value);         if (match) {             return match[1];         }     }); }).filter(x=>!!x); 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -