Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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,我有一个循环: for(设i=0;i{ log(`Line${index}:`); console.log(第1行); }); 如果(找到的长度>0){ 继续; } 行。推送(新行(ranVert1,ranVert2)); } 它循环遍历顶点数组中的每个顶点,并在任意两个顶点之间创建一条线。但我想删除重复项 如果我们有顶点0,1,2,3,4。然后生成第[2,4]行。我不希望它生成另一行,即[2,4]或[4,2] 最好的方法是什么?(最好不使用for循环) 提前谢谢 编辑: 顶点如下所示: {

我有一个循环:

for(设i=0;i{
log(`Line${index}:`);
console.log(第1行);
});
如果(找到的长度>0){
继续;
}
行。推送(新行(ranVert1,ranVert2));
}
它循环遍历顶点数组中的每个顶点,并在任意两个顶点之间创建一条线。但我想删除重复项

如果我们有顶点0,1,2,3,4。然后生成第[2,4]行。我不希望它生成另一行,即[2,4]或[4,2]

最好的方法是什么?(最好不使用for循环)

提前谢谢

编辑:

顶点如下所示:

{
   x: 300,
   y: 500
}
{
   v1: 5,
   v2: 6
}
let matrix = vertices.map(() => Array(vertices.length).fill(0));

for (let i = 0; i < vertices.length; i++) {
    let ranVert1 = floor(random(1, vertices.length - 1)) + 1;
    let ranVert2 = floor(random(1, vertices.length - 1)) + 1;

    let found = matrix[ranVert1][ranVert2] || ranVert1 == ranVert2;
    if (found) {
        continue;
    }
    matrix[ranVert1][ranVert2] = 1;
    matrix[ranVert2][ranVert1] = 1; // To avoid the other direction as well
    lines.push(new Line(ranVert1, ranVert2));
}
let lines = vertices.flatMap((v1, i) => 
    vertices.slice(i+1).map(v2 => new Line(v1, v2))
);
shuffle(lines); // see link for implementation of shuffle
lines.splice(vertices.length); // keep only first few elements
一行如下所示:

{
   x: 300,
   y: 500
}
{
   v1: 5,
   v2: 6
}
let matrix = vertices.map(() => Array(vertices.length).fill(0));

for (let i = 0; i < vertices.length; i++) {
    let ranVert1 = floor(random(1, vertices.length - 1)) + 1;
    let ranVert2 = floor(random(1, vertices.length - 1)) + 1;

    let found = matrix[ranVert1][ranVert2] || ranVert1 == ranVert2;
    if (found) {
        continue;
    }
    matrix[ranVert1][ranVert2] = 1;
    matrix[ranVert2][ranVert1] = 1; // To avoid the other direction as well
    lines.push(new Line(ranVert1, ranVert2));
}
let lines = vertices.flatMap((v1, i) => 
    vertices.slice(i+1).map(v2 => new Line(v1, v2))
);
shuffle(lines); // see link for implementation of shuffle
lines.splice(vertices.length); // keep only first few elements

创建一个零矩阵,其中行和列对应于第一个和第二个顶点的索引。当输出一对时,在该矩阵中输入1。如果已经有1,不要输出它

我假设您也希望避免像[2,2]这样的对,即创建一条到同一顶点的线

比如说:

{
   x: 300,
   y: 500
}
{
   v1: 5,
   v2: 6
}
let matrix = vertices.map(() => Array(vertices.length).fill(0));

for (let i = 0; i < vertices.length; i++) {
    let ranVert1 = floor(random(1, vertices.length - 1)) + 1;
    let ranVert2 = floor(random(1, vertices.length - 1)) + 1;

    let found = matrix[ranVert1][ranVert2] || ranVert1 == ranVert2;
    if (found) {
        continue;
    }
    matrix[ranVert1][ranVert2] = 1;
    matrix[ranVert2][ranVert1] = 1; // To avoid the other direction as well
    lines.push(new Line(ranVert1, ranVert2));
}
let lines = vertices.flatMap((v1, i) => 
    vertices.slice(i+1).map(v2 => new Line(v1, v2))
);
shuffle(lines); // see link for implementation of shuffle
lines.splice(vertices.length); // keep only first few elements

注意:在代码中,您生成
顶点。长度
行,但您也会写“…在任意两个顶点之间创建一条线”。它不会在任何顶点之间创建一条线,因为这样的线可能比
顶点多。长度
:实际上有
顶点。长度*(顶点.长度-1)/2
这样的线可能。如果您真的想要所有这些,那么一定要选择第二个解决方案,因为第一个解决方案可能需要一些时间来随机生成生成过程结束时剩下的唯一一个解决方案。
shuffle
算法没有这个问题,然后您可以省略
splice

将此作为答案,以便我可以接受@trincotok。张贴。