user838838
user838838

Reputation:

Compare two strings in javascript/jquery

In the string below, I want to split the string separated by "|" (which i can do)and would then like to compare the first part and the boolean part of each such string with the same parts of another string.

var str = "Jacobs21]2]0]Ronan]false|Tom]2]0]Joseph]true|Jacobs21]2]0]NAME$$ALL]false|";
  1. In the string above, Jacobs21]2]0]Ronan]false is one string and so on.
  2. I am more interested in the Jacobs21 part of this string and the boolean value appearing at its end which is "false" here.
  3. Now, I want to compare the first and the last part joined as a single string to form Jocobs21false and similarly, for the another string tomtrue and make a comparison and see if there are any similar matches?

Upvotes: 0

Views: 1164

Answers (1)

Sajad Karuthedath
Sajad Karuthedath

Reputation: 15847

var detailsArray = str.split("|");
var res = [];
for (var i = 0; i < detailsArray.length - 1; i++) {
    finalArray = detailsArray[i].toString().split("]");
    var name = finalArray[0];
    var booleanString = finalArray[4];
    res[i] = name.concat(booleanString);
}
for (var j = 0; j < res.length - 1; j++) {
    if (res[i] == res[i + 1]) {
        //do your stuff
    }
}

Upvotes: 2

Related Questions