Vlasta Po
Vlasta Po

Reputation: 901

JavaScript Search and Loop - doesn't return correct values

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

Upvotes: 0

Views: 71

Answers (2)

Artem Yavorskyi
Artem Yavorskyi

Reputation: 46

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});
 });

Upvotes: 0

trincot
trincot

Reputation: 350137

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.

Upvotes: 2

Related Questions