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
Post a Comment