0

Please, can you check my code where is the error? It should loop trough 1 array to choose each string and then loop through second array and check, if the value from second string contains value of first string.

  for (var i = 0; i < oldLines.length; i++){
    var subStringEach = oldLines[i];
    var subStringEachNoDash = subStringEach.replace(/[^a-z0-9]/g,'');

    // read New URLs and line by line save them as an object
    var newLines =  $('#newUrl').val().split(/\n/);
    var newUrlResult = [];

    for (var j = 0; j < newLines.length; j++){
      var newUrlString = newLines[j];
      var newUrlStringNoDash = newUrlString.replace(/[^a-z0-9]/g,'');

      var isThere = newUrlStringNoDash.search(subStringEachNoDash);
      if (isThere !== -1 ) {
        newUrlResult[i] = newLines[j];
      }
      else {
        newUrlResult[i] = "";
      }
    }

stockData.push({OldURL:oldLines[i],SearchSubstring:subStringEach,NewURL:newUrlResult[i]});
  }

Now it finds only part of the results.. I place to first array:

anica-apartment
casa-calamari-real
ostrovni-apartman

and to the second array:

http://tempweb3.datastack.cz/be-property/anica-apartment/
http://tempweb3.datastack.cz/be-property/ostrovni-apartman/
http://tempweb3.datastack.cz/be-property/st-michael-apartment/
http://tempweb3.datastack.cz/be-property/casa-calamari-real/

and it will only find and return casa-calamari-real, http://tempweb3.datastack.cz/be-property/casa-calamari-real/ and the others returns empty..

Any ideas please?

Here is the full code on Codepen: https://codepen.io/vlastapolach/pen/VWRRXX

2
  • Here's a working version: jsfiddle.net/khrismuc/xh8y14jL Commented Jul 15, 2017 at 12:03
  • Thank you @ChrisG. It is finally working :) Commented Jul 15, 2017 at 12:18

2 Answers 2

2

Once you find a match you should exit the inner loop, otherwise the next iteration of that loop will clear again what you had matched.

Secondly, you should use push instead of accessing an index, as you don't know how many results you will have. And as a consequence you will need to relate the find string with it (because i will not be necessary the same in both arrays)

So replace:

  if (isThere !== -1 ) {
    newUrlResult[i] = newLines[j];
  }
  else {
    newUrlResult[i] = "";
  }

with this:

  if (isThere !== -1 ) {
    newUrlResult.push({
        searchSubstring: subStringEach, 
        newURL: newUrlString
    });
    break; // exit loop
  }

At the end, just output newUrlResult.

NB: If you want to leave the possibility that a search string matches with more than one URL, then you don't need the break. The push will then still prevent you from overwriting a previous result.

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

1 Comment

Thank you so much! adding the break solved it. So simple..:) Thanks!
0

I see that you solved already) But maybe you will like this code too) newUrlResult variable could be a string I guess, because loop breaks when one value is found. If no values where found there will be just empty string. And I'm not sure you need to call newLines = $('#newUrl').val().split(/\n/) on every iteration.

var stockData = [];
 
 oldLines.map(function(oldLine){
      var cleanOldLine = oldLine.replace(/[^a-z0-9]/g,''),
          newLines =  $('#newUrl').val().split(/\n/),
          newUrlResult = '';
      
      for (var j = 0; j < newLines.length; j++){
      	var newLine = newLines[j],
            cleanNewLine = newLine.replace(/[^a-z0-9]/g,''),
            ifExists = cleanNewLine.search(cleanOldLine);
        
        if (ifExists !== -1) {
            newUrlResult = newLine;
            break;
        }
      }
      
      stockData.push({OldURL:oldLine, SearchSubstring:cleanOldLine, NewURL:newUrlResult});
 });

1 Comment

Hi, thank you for the tip. Now it is working and here is full code if you are curious:) github.com/vlastapolach/SEO-Redirect It is working with text box where you paste list of URLs and each URL is on new line and it is needed to show the found match or blank space if not found with the exact line order. Everything is dependent on the user input, there are no "hard-coded" strings. It should be useful for SEO specialist, feel free to download it and use it, if you like :)

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.