https://jsfiddle.net/vr4Lbc6p/1/

https://jsfiddle.net/vr4Lbc6p/1/
why there is an error , while regex applied on the array element.
why getting undefined ??

I have moved your post to a more suitable sub-forum.

To answer your question: Remember to declare your variables, and it would be better to console.log within the map function, or assign the mapping to a variable and console.log that.

let arrayRegex = /.yal/;
array.map(e => {
	console.log(e.match(arrayRegex));
});

Hope this helps

1 Like

if i write this code then it will also give the same output
I thought this solution from net but can not be able to understand,
Why we have to re return the match.

let array = ["Neeyal","priyal","Miyal","jiyal","parag","payal"];
arrayRegex = /.yal/;
console.log(array.map(e =>{
return e.match(arrayRegex);
}));

In line4 why we rereturn .
whether match method itself returning value.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

1 Like

In this case, it does not have to do with the match method, but the way arrow functions work. What you have with the return is the same as this:

console.log(array.map(e => e.match(arrayRegex)));

If you include the { } in an arrow function, you have to explicitly define what needs to be returned. Otherwise, the map receives no objects to put into an array. Hence, undefined.

1 Like