0

How can I compare every URL with those which already in an array, but only by their GET parameters names(not values).

For example:

var urlArray = [
"http://example.org/page.php?param1=123&param2=234",
"http://example.org/page.php?param3=123&param4=234",
"http://example.org/page.php?param5=123&param6=234",
];

var newUrl = "http://example.org/page.php?param1=123&param2=234&param3=345";

addingFlag = 1;

for (a = 0; a < urlArray.length; a++){
    var LinkFromArray = urlArray[a].replace(/=.*\&|=.*/, "");
    var LinkToArray = newUrl.replace(/=.*&|=.*/, "");

    if (LinkFromArray.indexOf(LinkToArray) >= 0){ 
        addingFlag = 0;
    }
}

if (addingFlag == 1){
    urlArray.push(newUrl);
}

This example should add newUrl to urlArray but should not add this URL: "http://example.org/page.php?param5=777&param6=000"

passwd
  • 2,883
  • 3
  • 12
  • 22
  • 1
    Just [parse the url](http://stackoverflow.com/q/901115/1048572), merge the pieces, and put it back together? – Bergi Sep 20 '16 at 13:06

1 Answers1

0
var urlArray = ['http://example.org/page.php?param1=123&param2=234','http://example.org/page.php?param3=123&param4=234','http://example.org/page.php?param5=123&param6=234','http://example.org/page.php?param6=890&param5=765'];

var uniqueUrls = [urlArray[0]];

for (var a = 1; a < urlArray.length; a++) {

    var params = urlArray[a].split("?")[1].split("&");
    for (var c = 0; c < uniqueUrls.length; c++) {

        var exists = [];
        for (var b = 0; b < params.length; b++) {

            var param = params[b].split("=")[0];
            if (uniqueUrls[c].indexOf("?" + param + "=") > -1 || uniqueUrls[c].indexOf("&" + param + "=") > -1)
                exists.push(1);
        }
    }

    if (exists.length != params.length)
        uniqueUrls.push(urlArray[a]);
}

there could be a case like this ?param6=1&param6=2, it is unique but since it was not part of the question I left it out, might have some time to update for that later tonight.

vlscanner
  • 448
  • 5
  • 16