-1

I have a variable as var input = 'promojam-untitled-promotion-2'; and an array

var prefferedPatterns = [
  "https://promojam.live.promojam.dev:5000/promojam-untitled-promotion-2",
  "http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-3",
  "http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-4",
  "http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-5",
  "http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-6",
  "http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-7",
  "http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-8",
  "http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-9"
]

I have to find the matched input element from this array. any idea ?

2
  • 1
    Have you tried anything? for, indexOf, filter, RegEx, ... Commented Jan 19, 2016 at 10:32
  • stackoverflow.com/questions/3074700/… Commented Jan 19, 2016 at 10:34

2 Answers 2

1

You will have to loop through array and search input in current value using indexOf().

Following is a sample code using .filter()

var input = 'promojam-untitled-promotion-2';

var prefferedPatterns = [
  "https://promojam.live.promojam.dev:5000/promojam-untitled-promotion-2",
  "http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-3",
  "http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-4",
  "http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-5",
  "http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-6",
  "http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-7",
  "http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-8",
  "http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-9"
]

var output = prefferedPatterns.filter(function(item){
  return (item.indexOf(input)>0)
})

console.log(output);

Sign up to request clarification or add additional context in comments.

Comments

0

Try this.

var input = 'promojam-untitled-promotion-2';
var prefferedPatterns = [
    "https://promojam.live.promojam.dev:5000/promojam-untitled-promotion-2",
    "http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-3",
    "http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-4",
    "http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-5",
    "http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-6",
    "http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-7",
    "http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-8",
    "http://promojam.live.promojam.dev:5000/promojam-untitled-promotion-9"
     ]
      for (var i = 0; i < prefferedPatterns.length; i++) {
        if (prefferedPatterns[i].indexOf(input) > -1) {
        alert(prefferedPatterns[i]);
      }
     }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.