Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.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 - Fatal编程技术网

Javascript 如何检测数组中的重复字符,并相应地创建参数?

Javascript 如何检测数组中的重复字符,并相应地创建参数?,javascript,Javascript,晚上好,我正在尝试检测字符串中的重复字符。更具体地说,我试图在一个数组中找到最多两个不同的副本。如果有一个重复项,则添加一个子字符串;如果有另一个重复项,则添加另一个子字符串。有没有办法做到这一点 以下是我迄今为止的一些示例代码: var CodeFieldArray = ["Z80.0", "Z80.1", "Z80.0", "Z70.4"]; /* We have an array here used to create the final string at the end of th

晚上好,我正在尝试检测字符串中的重复字符。更具体地说,我试图在一个数组中找到最多两个不同的副本。如果有一个重复项,则添加一个子字符串;如果有另一个重复项,则添加另一个子字符串。有没有办法做到这一点

以下是我迄今为止的一些示例代码:

var CodeFieldArray = ["Z80.0", "Z80.1", "Z80.0", "Z70.4"]; 

/* We have an array here used to create the final string at the end of the 
code.  It is a dummy array with similar variables in my actual code.  For 
reference sake, there may be only one object in the array, or 7 total, 
depending on the user's input, which is where the duplicate detection should 
come in, in case the user enters in multiples of the same code. */

var i, Index;

for (i = 0, L = 0; i < CodeFieldArray.length; i++) {  
  Index = CodeFieldArray[i].indexOf(CodeFieldArray[i]);
  if(Index > -1) L += 1;
  Extra0 = CodeFieldArray.indexOf("Z80.8");
  Extra1 = CodeFieldArray.indexOf("Z80.9");
  if(L >= 2 && Extra0 == -1) CodeFieldArray.push("Z80.8");
  Extra0 = CodeFieldArray.indexOf("Z80.8");
  if(L >= 4 && Extra0 != -1 && Extra1 == -1) CodeFieldArray.push("Z80.9");
  console.println(Extra0);
}

/*^ we attempted to create arguments where if there are duplicates 
'detected', it will push, "Z80.8" or, "Z80.9" to the end of the Array.  They 
get added, but only when there are enough objects in the Array... it is not 
actually detecting for duplicates within the Array itself^*/

function UniqueCode(value, index, self) { 
    return self.indexOf(value) === index;
}
CodeFieldArray = CodeFieldArray.filter(UniqueCode);
FamilyCodes.value = CodeFieldArray.join(", ");

/* this is where we turn the Array into a string, separated by commas.  The expected output would be "Z80.0, Z80.1, Z70.4, Z80.8"*/
var CodeFieldArray=[“Z80.0”、“Z80.1”、“Z80.0”、“Z70.4”];
/*这里有一个数组,用于在
代码。它是一个虚拟数组,在我的实际代码中具有类似的变量。对于
为了引用,数组中可能只有一个对象,或者总共有7个,
根据用户的输入,重复检测应该在哪里进行
如果用户输入相同代码的倍数,请输入*/
var i,指数;
对于(i=0,L=0;i-1)L+=1;
Extra0=CodeFieldArray.indexOf(“Z80.8”);
Extra1=CodeFieldArray.indexOf(“Z80.9”);
如果(L>=2&&extr0==-1)CodeFieldArray.push(“Z80.8”);
Extra0=CodeFieldArray.indexOf(“Z80.8”);
如果(L>=4&&extr0!=-1&&extr1==-1)CodeFieldArray.push(“Z80.9”);
控制台.println(extr0);
}
/*^我们试图创建参数,如果存在重复项
“检测到”,它会将“Z80.8”或“Z80.9”推送到阵列的末尾。他们
添加,但仅当数组中有足够的对象时。。。事实并非如此
实际检测阵列本身中的重复项^*/
函数唯一代码(值、索引、自身){
返回self.indexOf(value)==索引;
}
CodeFieldArray=CodeFieldArray.filter(UniqueCode);
FamilyCodes.value=CodeFieldArray.join(“,”);
/*在这里,我们将数组转换为字符串,用逗号分隔。预计产量为“Z80.0、Z80.1、Z70.4、Z80.8”*/

如果它们不存在,我会将它们添加到“Z80.8”或“Z80.9”的位置,但只有当数组中有足够的对象时,才会添加它们。我的for循环没有专门检测重复项本身。如果有一种方法专门检测重复项,并基于此创建一个参数,那么我们将做得很好。预期输出为“Z80.0、Z80.1、Z70.4、Z80.8”

您可以执行以下操作:

var uniqueArray = function(arrArg) {
  return arrArg.filter(function(elem, pos,arr) {
    return arr.indexOf(elem) == pos;
  });
};

uniqueArray ( CodeFieldArray  )
您可以使用和

var CodeFieldArray=[“Z80.0”、“Z80.1”、“Z80.0”、“Z70.4”];
let unique=[…新集合(CodeFieldArray)];
让match=['Z80.8','Z80.9'];
设numOfDup=CodeFieldArray.length-unique.length;
如果(numOfDup){
match.forEach(e=>{
if(!unique.包括(e)和&numodup){
推(e);
numOfDup--;
}
})
}

console.log(unique.join(‘,’)
和您的预期输出是?您是否尝试将
1
添加到输入的小数部分?预期输出是一个字符串,其中我们使用“CodefieldArray.join”(“,”);其思想是格式化数组,以便搜索重复项,并将代码“Z80.8”和“Z80.9”推送到数组中。(如果它们在数组中不存在)之后,数组将转换为字符串。如果数组中有两个值是
“Z80.8”
推送
“Z80.9”
数组,则预期输出将是“Z80.0、Z80.1、Z70.4、Z80.8”@RyanOliver?请注意,问题
.push()处的代码
for
循环中迭代同一数组的值,这会改变
CodeFieldArray.length
。你能在问题中包括有效和无效输入和输出的测试用例吗?请看@RyanOliver我很困惑。下面是一些我想知道的事情,如果它们('Z80.8`)是否已经存在比应该推送的值更多的值?是否要删除所有重复项?如何添加,代码段说它可以工作,但我的程序说let声明中缺少;遗憾的是。