foodiepanda
foodiepanda

Reputation: 138

How to replace part of string with array items in jQuery?

I have patch of code as below:

var originalText = "[email protected];[email protected];[email protected];";
var compareArr = ["[email protected];[email protected]","[email protected];[email protected]"];
var result = "";

Here, each item of originalText has to be compared with each items in array compareArr. If first letter of item in an array is matched, then it should replace original string with item in array.

In above example, '[email protected];;' is present in array, so '[email protected];' would be replaced by '[email protected];[email protected];'. Again, '[email protected];' is present in array, so '[email protected];' would be replaced by '[email protected];[email protected];'.

Final result that I want is '[email protected];[email protected];[email protected];[email protected];[email protected];'

This is what I have tried so far which gives me only first string '[email protected];[email protected];' in output.

var originalText = "[email protected];[email protected];[email protected];";
var compareArr = ["[email protected];[email protected]","[email protected];[email protected]"];
var result = "";

var arr1 = originalText.split(";");
for (x=0;x<arr1.length-1;x++)
{
    for(y=0;y<compareArr.length;y++)
    {
        var arr2 = compareArr[y].split(";");
        if(arr2[y] == arr1[x])
        {
            result += compareArr[y];
        }
    }
}
console.log(result);

I expected output to be '[email protected];[email protected];[email protected];[email protected];[email protected];' , but I am getting '[email protected];[email protected];;'.

Upvotes: 0

Views: 143

Answers (4)

foodiepanda
foodiepanda

Reputation: 138

Thank you @Gowri, As per your suggestion, I modified my code. Also, I missed adding remaining string which I have added again. Below is my final working code.

var originalText = "[email protected];[email protected];[email protected];[email protected];";
var compareArr = ["[email protected];[email protected]","[email protected];[email protected];"];
var result = "";
var resultArr = [];

var arr1 = originalText.split(";");
for (x=0;x<arr1.length-1;x++)
{
    for(y=0;y<compareArr.length;y++)
    {
        var arr2 = compareArr[y].split(";");
        if(arr2[0] == arr1[x])
        {
            result += compareArr[y];
        }
        else
        {
            result+=arr1[x] + ";";
        }
    }
}

function unique(list)
{
    var result = [];
    $.each(list, function(i, e) {
        if ($.inArray(e, result) == -1) result.push(e);
    });
    return result;
}

resultArr = result.split(";");
resultArr = unique(resultArr);
resultArr = resultArr.filter(function(X){return X!==''});
console.log(resultArr);

Upvotes: 0

Santosh Sarnobat
Santosh Sarnobat

Reputation: 1

enter code here
var originalText = "[email protected];[email protected];[email protected];";
var compareArr = ["[email protected];[email protected]","[email protected];[email protected]"];
var result = "";

var arr1 = originalText.split(";");
for (x=0;x<arr1.length-1;x++)
{
   for(y=0;y<compareArr.length;y++)
   {
      var arr2 = compareArr[y].split(";");
      if(arr2[0] == arr1[x])
      {
       result += compareArr[y];
   }
  else
        {
       var test = arr1[x];
       if(result.indexOf(test) != -1)
       {
        result;
       }
       else
       {
        result += ";" + test;
        }

        } 
     }
}
console.log(result);

Upvotes: 0

Gowri
Gowri

Reputation: 1856

In Your second loop if condition is wrong. You have to check with first element i.e 0 index, but you checked with y. It's causing prob.

var arr1 = originalText.split(";");
for (x=0;x<arr1.length-1;x++)
{
    for(y=0;y<compareArr.length;y++)
    {
        var arr2 = compareArr[y].split(";");
        if(arr2[0] == arr1[x])
        {
            result += compareArr[y]+';';
        }
    }
}

Upvotes: 1

Maur&#237;cio Araldi
Maur&#237;cio Araldi

Reputation: 389

What happens is that you are using the index in the comparison, and you're getting the wrong character. It is easier if I show you:

var originalText = "s;y;g;";
var compareArr = ["s;u;","y;o;"];
var result = "";

var arr1 = originalText.split(";");
for (x=0;x<arr1.length-1;x++) // Will run through s, y and g
{
    for(y=0;y<compareArr.length;y++) // For each character (s,y,g) will run through "s;u;","y;o;"
    {
        var arr2 = compareArr[y].split(";");
        if(arr2[y] == arr1[x])
        {
            result += compareArr[y];
        }
    }
}
console.log(result);

Let's examine

  • When first for is s [x = 0]:
    • Second for is s;u; [y = 0];
      • arr2 = ['s', 'u']
      • Your if does (arr2[0] == arr1[0]), which is (s == s) - It gets inside for
    • Second for is y;o; [y = 1]
      • arr2 = ['y', 'o']
      • Your if does (arr2[1] == arr1[0]), which is (o == s) - Doesn't get inside IF ---> Here is the problem, as in your IF condition you're just using the same Y index, it will never use the "y" character from compareArr[1].

So the solution is, in the if condition, always use the index 0, like if (arr2[0] == arr1[x]. But if you need to compare also the second letter, then you need to refactor your code a little.

In general, your code is a little bit confusing, I would advice using some techniques or some Array.methods to improve it.

Upvotes: 1

Related Questions