Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否删除基于部分内容的重复数组元素?_Javascript_Arrays - Fatal编程技术网

Javascript 是否删除基于部分内容的重复数组元素?

Javascript 是否删除基于部分内容的重复数组元素?,javascript,arrays,Javascript,Arrays,编辑: 上下文:我继承了一个进程(来自一位前同事),该进程生成一个通用文件,其中包括创建以下项目列表。该列表稍后需要转换为一系列无序链接,并保留嵌套级别 从下面的数组中,我需要删除重复项,不管它根据href属性的值显示多少次 var array = [ '<tag href="cheese.html">', '<tag href="cheddar.html"></tag>', ' <tag href="cheese.html"></t

编辑:

上下文:我继承了一个进程(来自一位前同事),该进程生成一个通用文件,其中包括创建以下项目列表。该列表稍后需要转换为一系列无序链接,并保留嵌套级别

从下面的数组中,我需要删除重复项,不管它根据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>',
];
var数组=[
'',
'',
'  ',
'',
'',
' ',
'   '
' '
''
'',
'  ',
'    ',
'  ',
'',
'',
'  ',
'  ',
'    ',
'    ',
'      ',
'      ',
'        ',
'      ',
'    ',
'  ',
'',
];
阵列删除所有重复项后,应如下所示:

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

从这里,我提取生成无序链接列表所需的信息没有问题。我只是想知道如何删除重复项。

了解问题的背景会很有帮助

此函数返回具有唯一href值的所有字符串,但不管理结束标记。删除结束标记将是一项复杂的任务。另外,我很确定用正则表达式解析HTML是可行的


如果你已经描述了你到底想做什么,那么必须有另一种方法来解决你的问题。

了解问题的背景会很有帮助

此函数返回具有唯一href值的所有字符串,但不管理结束标记。删除结束标记将是一项复杂的任务。另外,我很确定用正则表达式解析HTML是可行的


如果你已经描述了你想要实现的目标,那么必须有另一种方法来解决你的问题。

为了更容易理解,这里有一个专门的详细解决方案。我假设没有
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">' ]
  */
var-arr=[
'',
'',
'  ',
'',
'',
' ',
'   ',
' ',
''
];
//删除数组中每个字符串两端的空白
//这不是一个必要的步骤,但为了方便起见,只需以这种方式处理前导空格和尾随空格
arr=arr.map(函数(标记字符串){
返回tagString.trim();
}); 
//Regex从标记中检索href值
var hrefRegexp=/(\s+href=\”)([^\“]+)(\”)/g;
//仅使用href值创建一个数组,以便于查找
hrefArr=arr.map(函数(标记字符串){
//对标记字符串运行regex
var href=hrefRegexp.exec(标记字符串);
//重置“RegExp”的索引
hrefRegexp.lastIndex=0;
//如果未找到href匹配项,则返回null,
如果(href==null)返回null;
//否则,返回href值
否则返回href[2];
});
//存储数组长度(此值将在下面的for循环中使用)
var arrLength=arr.length;
//从左侧开始,比较右侧的值
对于(变量leftCompareIndex=0;leftCompareIndex
为了更容易理解,这里有一个专门的详细解决方案。我假设没有
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">' ]
  */
var-arr=[
'',
'',
'  ',
'',
'',
' ',
'   ',
' ',
''
];
//删除数组中每个字符串两端的空白
//这不是一个必要的步骤,但为了方便起见,只需以这种方式处理前导空格和尾随空格
arr=arr.map(函数(标记字符串){
返回tagString.trim();
}); 
//Regex从标记中检索href值
var hrefRegexp=/(\s+href=\”)([^\“]+)(\”)/g;
//仅使用href值创建一个数组,以便于查找
hrefArr=arr.map(函数(标记字符串){
//对标记字符串运行regex
var href=hrefRegexp.exec(标记字符串);
//重置“RegExp”的索引
hrefRegexp.lastIndex=0;
//如果未找到href匹配项,则返回null,
如果(href==null)返回null;
//否则,返回href值
否则返回href[2];
});
//存储数组长度(此值将在下面的for循环中使用)
var arrLength=arr.length;
//从左侧开始,比较右侧的值
对于(变量leftCompareIndex=0;leftCompareIndex