检查对象中的重复项

时间:2017-06-15 08:04:08

标签: javascript

我有一个这样的结构,我想检查数组中是否有重复的URL,这样我就可以根据Keyword属性定位那些并生成一些条件语句。提前谢谢。

{
   "prochat": [{
          "Title": "Multiple KB Page",
          "Message": "Hi, Multiple KB Page?",
          "ProblemDescription": "Multiple KB Page",
          "Keyword": "",
          "Template": 1,
          "URL": "www.abcd.com"
        },  
       {
        "Title": "URL 1",
        "Message": "Do you want to Renew?",
        "ProblemDescription": "Message about Installing",
        "Keyword": "Renewals",
        "Template": 1,
        "URL": "www.nba.com"
     },

    {
        "Title": "URL 1",
        "Message": "Do you want to Install?",
        "ProblemDescription": "Message about Installing",
        "Keyword": "Installings",
        "Template": 1,
        "URL": "www.nba.com"
    }
  ]
}

我有这样的条件声明

if (window.location.href.indexOf(data.prochat[i].URL) > -1) { // logic here }

基本上我需要匹配网址,如果匹配,我会显示一个按钮。但是也有一些重复的网址,所以我想如果有重复,我将依赖于关键字属性,如果它有相同的网址。

2 个答案:

答案 0 :(得分:0)

迭代您的数据,如果URL是新的,只需将其添加到新数组即可。如果它已经在其中,请按照您的意愿处理,因为它是重复的。

您可以尝试以下代码:

var myJSON = {
   "prochat": [{
          "Title": "Multiple KB Page",
          "Message": "Hi, Multiple KB Page?",
          "ProblemDescription": "Multiple KB Page",
          "Keyword": "",
          "Template": 1,
          "URL": "www.abcd.com"
        },  
       {
        "Title": "URL 1",
        "Message": "Do you want to Renew?",
        "ProblemDescription": "Message about Installing",
        "Keyword": "Renewals",
        "Template": 1,
        "URL": "www.nba.com"
     },

    {
        "Title": "URL 1",
        "Message": "Do you want to Install?",
        "ProblemDescription": "Message about Installing",
        "Keyword": "Installings",
        "Template": 1,
        "URL": "www.nba.com"
    }
  ]
};

    var duplicateURLs = [];
    for(var key in myJSON.prochat){
        if(myJSON.prochat.hasOwnProperty(key)){
        if(duplicateURLs.indexOf(myJSON.prochat[key].URL) > 0){
            // URL is duplicate, do stuff
        }
        else{
            duplicateURLs.push(myJSON.prochat[key].URL);
        }
      }
    }

您可以在jsfiddle上在线试用。

答案 1 :(得分:0)

var newarray=[];
var object={
   "prochat": [{
          "Title": "Multiple KB Page",
          "Message": "Hi, Multiple KB Page?",
          "ProblemDescription": "Multiple KB Page",
          "Keyword": "",
          "Template": 1,
          "URL": "www.abcd.com"
        },  
       {
        "Title": "URL 1",
        "Message": "Do you want to Renew?",
        "ProblemDescription": "Message about Installing",
        "Keyword": "Renewals",
        "Template": 1,
        "URL": "www.nba.com"
     },

    {
        "Title": "URL 1",
        "Message": "Do you want to Install?",
        "ProblemDescription": "Message about Installing",
        "Keyword": "Installings",
        "Template": 1,
        "URL": "www.nba.com"
    }
  ]
}
console.log(object.prochat.length);
for(var i=0;i<object.prochat.length;i++){
    for(var j=i+1;j<object.prochat.length;j++){
    if(object.prochat[i].URL==object.prochat[j].URL){
    console.log("url matches"+i+j);
    newarray.push(object.projact[i].URL);
  }
}