How to find pattern in javascript variable and return the founded element in array?

atsang01

I will need your help to extract string pattern inside the JavaScript variable from the array.

For example, lets say:

  • var type = ["men","women","boys", "girls"]

  • var product = "women shoes"

I would like to write the function that search the variable product from my custom define variable type.

So the function can return the string "women".

Many thanks!!!

Allen

Cezary Tomczyk

I'd go with:

var type = ["men", "women", "boys", "girls"],
    product = "women shoes",
    result;

function filterData(item, index) {
   if (new RegExp( '\\b' + item + '\\b', 'i').test(product)) {
       return true;
   } 
   return false;
}

result = type.filter(filterData);
console.log(result);

Working example: https://jsfiddle.net/nc1qodk5/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to proxy context of each element founded through jQuery.find() inside a function?

From Dev

how to find middle element of array in javascript?

From Dev

How do I use a variable to find an element in an array?

From Dev

How to return pattern-matched element?

From Dev

How to return pattern-matched element?

From Dev

Find if an element exists in Javascript array

From Dev

Move array element to variable in JavaScript

From Dev

Move array element to variable in JavaScript

From Dev

How to find the row and column of a specific element in a multidimensional array using javascript

From Dev

How to find if an array element contains a specific value in JavaScript/jQuery?

From Dev

How to find the index of an element in a multidimensional array by JavaScript/JQuery

From Dev

how to find specific html element pattern

From Dev

How to return a specific element of an array?

From Dev

find index of array element in another array javascript

From Dev

find index of array element in another array javascript

From Dev

How to find a substring in a string variable using a pattern?

From Dev

Ruby split array then return first element based on pattern

From Dev

How to find an element in an array in mongodb?

From Dev

How to return index and value of the specific element from array on callback using any JavaScript array function?

From Dev

How to find pattern groups in boolean array?

From Dev

Find pattern in array of array

From Dev

Converting a variable value to (return) array in javascript?

From Dev

Find element in array using recursion - javascript

From Dev

Find element in array using recursion - javascript

From Dev

Return total of a GTM JavaScript variable (array) in another GTM JavaScript variable

From Dev

How do I find element in array object and update source array in Javascript

From Dev

How to get/find the function being called from a DOM element or variable in javascript or jquery

From Dev

how to remove javascript array element?

From Dev

How to get, return and delete a random element in an array?

Related Related

  1. 1

    How to proxy context of each element founded through jQuery.find() inside a function?

  2. 2

    how to find middle element of array in javascript?

  3. 3

    How do I use a variable to find an element in an array?

  4. 4

    How to return pattern-matched element?

  5. 5

    How to return pattern-matched element?

  6. 6

    Find if an element exists in Javascript array

  7. 7

    Move array element to variable in JavaScript

  8. 8

    Move array element to variable in JavaScript

  9. 9

    How to find the row and column of a specific element in a multidimensional array using javascript

  10. 10

    How to find if an array element contains a specific value in JavaScript/jQuery?

  11. 11

    How to find the index of an element in a multidimensional array by JavaScript/JQuery

  12. 12

    how to find specific html element pattern

  13. 13

    How to return a specific element of an array?

  14. 14

    find index of array element in another array javascript

  15. 15

    find index of array element in another array javascript

  16. 16

    How to find a substring in a string variable using a pattern?

  17. 17

    Ruby split array then return first element based on pattern

  18. 18

    How to find an element in an array in mongodb?

  19. 19

    How to return index and value of the specific element from array on callback using any JavaScript array function?

  20. 20

    How to find pattern groups in boolean array?

  21. 21

    Find pattern in array of array

  22. 22

    Converting a variable value to (return) array in javascript?

  23. 23

    Find element in array using recursion - javascript

  24. 24

    Find element in array using recursion - javascript

  25. 25

    Return total of a GTM JavaScript variable (array) in another GTM JavaScript variable

  26. 26

    How do I find element in array object and update source array in Javascript

  27. 27

    How to get/find the function being called from a DOM element or variable in javascript or jquery

  28. 28

    how to remove javascript array element?

  29. 29

    How to get, return and delete a random element in an array?

HotTag

Archive