0

How to find string Page 1 0f 5, Page 2 0f 5, Page 3 0f 5 from array and replace with space?At this time i added static condition but can i make it dynamic?

var arr = ["Page 1 0f 5","Page Cat", "Boy", "Page 2 0f 5", "Page 3 0f 5", "Page 4 0f 5", "Apple"];
    var myarray = [];
    for(var n=0;n<arr.length-1;n++){
        console.log(arr[n]);
        if(arr[n]!='Page 1 0f 5' || arr[n]!='Page 2 0f 5' || arr[n]!='Page 3 0f 5'){
            myarray.push(arr[n]);
        }
    }
2
  • You actually created new array, not replacing the items with space (as you stated above), but by removing those items instead. Please also be accurate, items may be Page 3 of 100, correct? Commented Nov 14, 2019 at 11:58
  • @skobaljic Yes page can be Page 3 of 100 Commented Nov 14, 2019 at 12:01

2 Answers 2

2

In order to do this dynamically, try and use some regex.

EG:

var arr = ["Page 1 0f 5","Page Cat", "Boy", "Page 2 0f 5", "Page 3 0f 5", "Page 4 0f 5", "Apple"];

var filteredArray = [];

for(var n=0;n<arr.length-1;n++){
 //matching any with Page ___ 0f ___
 res = arr[n].match(/Page [0-9]+ 0f [0-9]+/g);
 if(!res){
     //if it didn't find the string put it in your filteredArray
     filteredArray.push(arr[n]);
 }
}

console.log(filteredArray);
Sign up to request clarification or add additional context in comments.

1 Comment

Correct one, similar on JSFiddle.
0

check it man.?

var arr = ["Page 1 0f 5","Page Cat", "Boy", "Page 2 0f 5", "Page 3 0f 5", "Page 4 0f 5","Apple"];
    var myarray = [];
    for(var n=0;n<arr.length-1;n++){
        console.log(arr[n]);
        debugger;
        if(arr[n]!='Page 1 0f 5' && arr[n]!='Page 2 0f 5' && arr[n]!='Page 3 0f 5'){
            myarray.push(arr[n]);
            
        }
    }
    console.log(myarray)

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.