0

I want to this : When the entered value matches the value of the my array , write the matches value.

so this is my code :

//my array as the following :

var checkNames = (document.getElementById("KPIorCounterList").value.split("\n").map(element => element.trim())).filter(v=>v != "");

my another array : myarray= ["RAB Video call drop %",RAB PS R99 drop % ","RAB PS HSDPA drop %"]

For example ; when write RAB* to textare, I should to see, starting with "RAB"

I guess, My code should be as follows:

  for (var i = 0; i < checkNames.length; i++) {
                    for (var j = 0; j < myarray.length; j++) {
           // var str = myarray[j].split(" "); // I am not sure for his.

            I want to this for here : (pseudo code)
            for example checkName[i] == RAB*
            if (checkName[i].match("match condition") == myarray[j])
            alert(myarray[j]);
            //I should show output myarray[j] == RAB Video call drop %",RAB PS R99 drop % ","RAB PS HSDPA drop %"
       }
    }

How can I do ? Please..

5
  • Don't really have an answer for you, but i think myarray["j"] should be myarray[j].. Commented Jul 1, 2016 at 7:06
  • checkNames is temp array? Commented Jul 1, 2016 at 7:21
  • No, different array @VladuIonut Commented Jul 1, 2016 at 7:27
  • your pseudo code is not so clear , which is temp array in there Commented Jul 1, 2016 at 7:29
  • sorry, I edited my code. @VladuIonut Commented Jul 1, 2016 at 7:32

2 Answers 2

1

function check(){
var checkNames = (document.getElementById("KPIorCounterList").value.split("\n").map(element => element.trim())).filter(v=>v != "");
var myarray = ["RAB Video call drop %", "RAB PS R99 drop % ", "RAB PS HSDPA drop %"]
for (var i = 0; i < checkNames.length; i++) {
  console.log("results for", checkNames[i])
  for (var j = 0; j < myarray.length; j++) {

      var matchString = myarray[j].match(new RegExp(checkNames[i].replace('*','.*')));
    if (matchString && myarray.indexOf(matchString[0])!==-1) {
      console.log(myarray[j]);
    }
    //I should show output myarray[j] == RAB Video call drop %",RAB PS R99 drop % ","RAB PS HSDPA drop %"
  }
}
  }
<textarea id="KPIorCounterList"></textarea>
<button onclick="check()">Check</button>

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

1 Comment

checkName came from 'textarea.value '. I want to this : textarea.value is array and if array eleman is : ''RAB*'' the above code should work. How can I control it ? please @VladuIonut
1

@eagle, I believe this is the input to checkNames. If so, here is my solution -

var checkNames = ['RAB*'];
var myarray = ["RAB Video call drop %","RAB PS R99 drop % ","RAB PS HSDPA drop %"];

for (var i = 0; i < checkNames.length; i++) {
    for (var j = 0; j < myarray.length; j++) {
        var formatRegExpr = checkNames[i].replace('*','.*');
        var re = new RegExp(formatRegExpr, 'g');
        alert(myarray[j].match(re));
    }
}

Accept this answer if it solves your requirement.

3 Comments

I'm trying to learn so var re = new RegExp(formatRegExpr, 'g'); What 'g' is? @dinesh
g - global search flag. Good place to learn - regexr-dot-com
checkName came from 'textarea.value '. I want to this : textarea.value is array and if array eleman is : ''RAB*'' the above code should work. How can I control it ? please @dinesh

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.