2017-03-09 84 views
0

編輯:根據部分內容刪除重複的數組元素?

上下文:我繼承了一個進程(來自前同事)生成一個通用文件,其中包括創建以下項目列表。該列表稍後需要轉化爲一系列保留嵌套級別的無序鏈接。

從以下數組中,我需要刪除重複項,無論基於href屬性的值顯示多少次。

var array = [ 
'<tag href="cheese.html">', 
'<tag href="cheddar.html"></tag>', 
' <tag href="cheese.html"></tag>', 
'</tag>', 
'<tag href="burger.html">', 
' <tag href="burger.html">', 
' <tag href="burger.html"></tag>' 
' </tag>' 
'</tag>' 
'<tag href="lettuce.html">', 
' <tag href="lettuce.html">', 
' <tag href="lettuce.html"></tag>', 
' </tag>', 
'</tag>', 
'<tag href="tomato.html">', 
' <tag href="tomato.html"></tag>', 
' <tag href="tomato.html">', 
' <tag href="tomato.html"></tag>', 
' <tag href="tomato.html">', 
'  <tag href="tomato.html"></tag>', 
'  <tag href="tomato.html">', 
'  <tag href="tomato.html"></tag>', 
'  </tag>', 
' </tag>', 
' </tag>', 
'</tag>', 
]; 

陣列已全部重複刪除後,它應該是這樣的:

'<tag href="cheese.html">', 
'<tag href="cheddar.html"></tag>', 
'</tag>', 
'<tag href="burger.html">', 
'</tag>', 
'<tag href="lettuce.html">', 
'</tag>', 

從這裏,我沒有問題提取我需要生成我的鏈接無序列表的信息。我只需要幫助搞清楚如何刪除重複。

+0

爲什麼最後會出現兩個''值? – subwaymatch

+0

一個標籤元素嵌套在另一個標籤元素中。 – Jawa

回答

2

這將有助於瞭解問題的背景。

此函數返回所有具有唯一href值的字符串,但對管理結束標記沒有任何作用。去除結束標記將是一項複雜的任務。另外我很確定用正則表達式解析HTML是not a good idea

function sortByHref (array) { 
    var hrefReg = new RegExp('href="(.*)"'); 
    var seen = {}; 
    var match, href; 
    return array.filter(function (x) { 
    match = hrefReg.exec(x); 
    if (match) { 
     href = match[1]; 
     if (seen.hasOwnProperty(href) && seen[href]) return false; 
     seen[href] = true; 
    } 
    return true; 
    }); 
} 

如果你已經描述了你到底想要完成什麼,那麼必須有另一種方法來解決你的問題。

+2

非常漂亮和優雅的解決方案。 – subwaymatch

+0

工作得很好,但就像你說的那樣,它對結束標籤沒有任何作用。 – Jawa

+0

我想我找到了一個解決方案,擴展了你所做的事情:創建了第二個數組,循環遍歷整個清理過的數組,並推送與你的函數的輸出數組不匹配的匹配的數組:'cleanedArray [i] .indexOf '')> -1'。在我的測試中,這將刪除任何在其前面有空格的結束標籤元素。我會進行更深入的測試並確認它是否有效。 乾杯! – Jawa

1

這是一個特意詳細的解決方案,以便於理解。我假設沒有href值的標籤將根據整個字符串簡單地刪除重複項。

var arr = [ 
    '<tag href="cheese.html">', 
    '<tag href="cheddar.html"></tag>', 
    ' <tag href="cheese.html"></tag>', 
    '</tag>', 
    '<tag href="burger.html">', 
    ' <tag href="burger.html">', 
    ' <tag href="burger.html"></tag>', 
    ' </tag>', 
    '</tag>' 
]; 

// Remove whitespaces on both ends from each string in array 
// Not a necessary step, but will just handle leading and trailing whitespaces this way for convenience 
arr = arr.map(function(tagString) { 
    return tagString.trim(); 
}); 

// Regex to retrieve href value from tags 
var hrefRegexp = /(\s+href=\")([^\"]+)(\")/g; 

// Create an array with just the href values for easier lookup 
hrefArr = arr.map(function(tagString) { 
    // Run regex against the tag string 
    var href = hrefRegexp.exec(tagString); 

    // Reset `RegExp`'s index 
    hrefRegexp.lastIndex = 0; 

    // If no href match is found, return null, 
    if (href === null) return null; 

    // Otherwise, return the href value 
    else return href[2]; 
}); 

// Store array length (this value will be used in the for loop below) 
var arrLength = arr.length; 

// Begin from the left and compare values on the right 
for (var leftCompareIndex = 0; leftCompareIndex < arrLength; leftCompareIndex++) { 
    for (var rightCompareIndex = leftCompareIndex + 1; rightCompareIndex < arrLength; rightCompareIndex++) { 

     // A flag variable to indicate whether the value on the right is a duplicate 
     var isRightValueDuplicate = false; 

     // If href value doesn't exist, simply compare whole string 
     if (hrefArr[leftCompareIndex] === null) { 
      if (arr[leftCompareIndex] === arr[rightCompareIndex]) { 
       isRightValueDuplicate = true; 
      } 
     } 

     // If href value does exist, compare the href values 
     else { 
      if (hrefArr[leftCompareIndex] === hrefArr[rightCompareIndex]) { 
       isRightValueDuplicate = true; 
      } 
     } 

     // Check flag and remove duplicate element from both original array and href values array 
     if (isRightValueDuplicate === true) { 
      arr.splice(rightCompareIndex, 1); 
      hrefArr.splice(rightCompareIndex, 1); 
      arrLength--; 
      rightCompareIndex--; 
     } 
    } 
} 

console.log(arr); 

/* Should output 
[ '<tag href="cheese.html">', 
    '<tag href="cheddar.html"></tag>', 
    '</tag>', 
    '<tag href="burger.html">' ] 
    */ 
+0

我喜歡這個解決方案,但它並沒有在''的最後一個結束標記中添加。 – Jawa