根据其内容的一部分删除重复数组元素



编辑:

上下文:我继承了一个过程(来自以前的同事)生成的通用文件,除其他外,还创建了以下项目列表。稍后,该列表将需要变成一系列无序的链接,并保留并保留嵌套级别。

从以下数组中,我需要删除重复项,无论它根据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>',

从这里开始,我在提取我无序的链接列表所需的信息时没有问题。我只需要弄清楚如何删除重复项的帮助。

知道您的问题的上下文会很有帮助。

此功能返回具有唯一HREF值的所有字符串,但在管理关闭标签方面无济于事。删除关闭标签将是一项复杂的任务。另外,我很确定将HTML与Regex一起解析不是一个好主意。

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;
  });
}

如果您已经描述了您要完成的工作。

,必须有另一种方法来解决您的问题。

这是一种有目的的冗长解决方案,可以更轻松地理解。我假设没有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">' ]
  */

最新更新