从数组中随机选择的项目中删除重复项在javascript中不起作用

木星

下面的代码有四个框,我randomly从数组项中选择了3个项。

我将随机选择的项目放在4个框内。

box002可以拖放到相应数量的三盒显示。然后blue digit在框中消失。

我有一个代码可以从随机选择的项目中删除重复项,

item = array2.splice(randomIndex,1);

但是当我将其插入到我的代码中时,rvalue()函数不会rselect随机值

如何解决呢?并使随机选择的项目没有重复项吗?

var items = [{
    label: '1',
    url: 'https://via.placeholder.com/75x75?text=1'
  },
  {
    label: '2',
    url: 'https://via.placeholder.com/75x75?text=2'
  },
  {
    label: '3',
    url: 'https://via.placeholder.com/75x75?text=3'
  },
  {
    label: '4',
    url: 'https://via.placeholder.com/75x75?text=4'
  },
  {
    label: '5',
    url: 'https://via.placeholder.com/75x75?text=5'
  },
  {
    label: '6',
    url: 'https://via.placeholder.com/75x75?text=6'
  },
  {
    label: '7',
    url: 'https://via.placeholder.com/75x75?text=7'
  },
  {
    label: '8',
    url: 'https://via.placeholder.com/75x75?text=8'
  },
  {
    label: '9',
    url: 'https://via.placeholder.com/75x75?text=9'
  },
  {
    label: '10',
    url: 'https://via.placeholder.com/75x75?text=10'
  }
];

var tempimages = [];
var array2 = [];
array2 = items.slice();

var backimgcount = 1;
var len = array2.length;

var item;
var displaycounter = 0;




var images = ['https://picsum.photos/200/300', 'https://picsum.photos/g/200/300'];
var index = 0;

function buildImage() {
  document.getElementById('content').style.backgroundImage = 'url(images/' + images[index] + ')';
}

function changeImage() {
  index++;
  if (index >= images.length) index = 0;
  document.getElementById('content').style.backgroundImage = 'url(images/' + images[index] + ')';
}


function rvalue() {


  elements = document.getElementsByClassName("box");

  elements = document.getElementsByClassName("box");

  for (var i = 0; i < elements.length; i++) {
    elements[i].style.backgroundColor = "#ff66ff";
    elements[i].style.border = "2px solid  #e3a89e ";

  }


  ptags = document.querySelectorAll('[name="values"]');
  boxtags = document.getElementsByClassName("box");

debugger;
  for (var index = 0; index < 3; index++) {
    randomIndex = Math.floor(Math.random() * array2.length)

    item = array2[randomIndex];
    //item = array2.splice(randomIndex, 1);
    
    ptags[index].style.visibility = "visible";
    ptags[index].textContent = item.label;
    boxtags[index].style.backgroundImage = 'url(' + item.url + ')'; 
    tempimages.push({
      data: item,
      index: randomIndex
    });

    ptags[index].dataset.itemIndex = randomIndex;

  }

  var tlen = tempimages.length;


}


function displayAllImages() {
  if (tempimages.length == 0) {

    rvalue();
  }


  function shuffle(array) {
    var currentIndex = array.length,
      temporaryValue, randomIndex;

    // While there remain elements to shuffle...
    while (0 !== currentIndex) {

      // Pick a remaining element...
      randomIndex = Math.floor(Math.random() * currentIndex);
      currentIndex -= 1;

      // And swap it with the current element.
      temporaryValue = array[currentIndex];
      array[currentIndex] = array[randomIndex];
      array[randomIndex] = temporaryValue;
    }

    return array;
  }


  var arr = tempimages;
  arr = shuffle(arr);

  item = arr.shift();
  image = document.getElementById('slide');
  image.src = item.data.url;
  image.dataset.itemIndex = item.index;


};

$(function() {

  displayAllImages();

});

function allowDrop(ev) {
  ev.preventDefault();
}

function drag(ev) {
  ev.dataTransfer.setData("Text", ev.target.id);
}

function drop(ev) {

  ev.preventDefault();
  var data = ev.dataTransfer.getData("Text");
  var el = document.getElementById(data);


  var x = document.getElementById("slide").dataset.itemIndex;
  var y = ev.target.dataset.itemIndex;



  if (x == y) {

    ev.currentTarget.style.backgroundColor = 'initial';
    ev.currentTarget.style.border = 'initial';

    var pParagraph = ev.currentTarget.firstElementChild;
   
    pParagraph.style.visibility = "hidden";

    item = this.item;
    var arrayvalue = item.dataindex;
    array2.splice(arrayvalue, 1);


    if (tempimages.length == 0) {
      rvalue();
      changeImage();
    }
    displayAllImages();
  } else {
    alert("WRONG PLACED");
  }

}
body {
  overflow: hidden;
}

.box {
  width: calc(33.3% - 4px);
  display: inline-block;
  border-radius: 5px;
  border: 2px solid #333;
  border: #000 border-color: #e6e600;
  margin: -2px;
  border-radius: 0%;

}

.box {
  height: 15vh;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  background-size:contain;
}

.box002 {
  position: absolute;
  top: 27.3vh;
  left: 72.98vw;
  cursor: pointer;
}

.box002 img {
  width: 14.0vw;
  height: 23.0vh;
  border-radius: 50%;
}
p{
font: "Courier New", Courier, monospace;
font-size:30px;

 color: rgba(0,0,0,0.6);
text-shadow: 2px 8px 6px rgba(0,0,0,0.2),
                 0px -5px 35px rgba(255,255,255,0.3);

color: #005ce6;
text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container2">
  <div class="containerr">
    <div class="pic" id="content">
      <div id="container">

        <div class="box" ondrop="drop(event)" ondragover="allowDrop(event)" id="10">
          <p name="values"></p>
        </div>
        <div class="box" ondrop="drop(event)" ondragover="allowDrop(event)" id="11">
          <p name="values"></p>
        </div>
        <div class="box" ondrop="drop(event)" ondragover="allowDrop(event)" id="12">
          <p name="values"></p>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="box002" draggable="true" ondragstart="drag(event)" id="2">
  <img src="" draggable="true" id="slide" border="rounded" />
</div>

大卫

您用于删除重复项的代码正在运行。

问题是您覆盖了item变量。

item = array2[randomIndex];
item = array2.splice(randomIndex, 1);

在上面的第一行中,itemarray2数组中随机选择的元素执行第二行后,item将成为包含该元素的数组

Array.prototype.splice()MDN

返回值
包含已删除元素的数组。如果仅删除一个元素,则返回一个元素的数组。如果没有删除任何元素,则返回一个空数组。

例如(注意括号):

item = array2[randomIndex];
console.log(item); // { "label": "2", "url": ... }
item = array2.splice(randomIndex, 1);
console.log(item); // [ { "label": "2", "url": ... } ]

其余代码需要一个对象,而不是数组,因此它失败。由于您不需要使用该返回的数组做任何事情,因此只需切换至:

item = array2[randomIndex];
array2.splice(randomIndex, 1);

然后,由于您已经在随机选择过程中删除了元素,因此当用户将鼠标.box002放在相应的框中时,不要这样做这意味着删除drop()函数中的以下行

array2.splice(arrayvalue, 1);

var items = [{
    label: '1',
    url: 'https://via.placeholder.com/75x75?text=1'
  },
  {
    label: '2',
    url: 'https://via.placeholder.com/75x75?text=2'
  },
  {
    label: '3',
    url: 'https://via.placeholder.com/75x75?text=3'
  },
  {
    label: '4',
    url: 'https://via.placeholder.com/75x75?text=4'
  },
  {
    label: '5',
    url: 'https://via.placeholder.com/75x75?text=5'
  },
  {
    label: '6',
    url: 'https://via.placeholder.com/75x75?text=6'
  },
  {
    label: '7',
    url: 'https://via.placeholder.com/75x75?text=7'
  },
  {
    label: '8',
    url: 'https://via.placeholder.com/75x75?text=8'
  },
  {
    label: '9',
    url: 'https://via.placeholder.com/75x75?text=9'
  },
  {
    label: '10',
    url: 'https://via.placeholder.com/75x75?text=10'
  }
];

var tempimages = [];
var array2 = [];
array2 = items.slice();

var backimgcount = 1;
var len = array2.length;

var item;
var displaycounter = 0;




var images = ['https://picsum.photos/200/300', 'https://picsum.photos/g/200/300'];
var index = 0;

function buildImage() {
  document.getElementById('content').style.backgroundImage = 'url(images/' + images[index] + ')';
}

function changeImage() {
  index++;
  if (index >= images.length) index = 0;
  document.getElementById('content').style.backgroundImage = 'url(images/' + images[index] + ')';
}


function rvalue() {


  elements = document.getElementsByClassName("box");

  elements = document.getElementsByClassName("box");

  for (var i = 0; i < elements.length; i++) {
    elements[i].style.backgroundColor = "#ff66ff";
    elements[i].style.border = "2px solid  #e3a89e ";

  }


  ptags = document.querySelectorAll('[name="values"]');
  boxtags = document.getElementsByClassName("box");

debugger;
  for (var index = 0; index < 3; index++) {
    randomIndex = Math.floor(Math.random() * array2.length)

    item = array2[randomIndex];
    array2.splice(randomIndex, 1);
    
    ptags[index].style.visibility = "visible";
    ptags[index].textContent = item.label;
    boxtags[index].style.backgroundImage = 'url(' + item.url + ')'; 
    tempimages.push({
      data: item,
      index: randomIndex
    });

    ptags[index].dataset.itemIndex = randomIndex;

  }

  var tlen = tempimages.length;


}


function displayAllImages() {
  if (tempimages.length == 0) {

    rvalue();
  }


  function shuffle(array) {
    var currentIndex = array.length,
      temporaryValue, randomIndex;

    // While there remain elements to shuffle...
    while (0 !== currentIndex) {

      // Pick a remaining element...
      randomIndex = Math.floor(Math.random() * currentIndex);
      currentIndex -= 1;

      // And swap it with the current element.
      temporaryValue = array[currentIndex];
      array[currentIndex] = array[randomIndex];
      array[randomIndex] = temporaryValue;
    }

    return array;
  }


  var arr = tempimages;
  arr = shuffle(arr);

  item = arr.shift();
  image = document.getElementById('slide');
  image.src = item.data.url;
  image.dataset.itemIndex = item.index;


};

$(function() {

  displayAllImages();

});

function allowDrop(ev) {
  ev.preventDefault();
}

function drag(ev) {
  ev.dataTransfer.setData("Text", ev.target.id);
}

function drop(ev) {

  ev.preventDefault();
  var data = ev.dataTransfer.getData("Text");
  var el = document.getElementById(data);


  var x = document.getElementById("slide").dataset.itemIndex;
  var y = ev.target.dataset.itemIndex;



  if (x == y) {

    ev.currentTarget.style.backgroundColor = 'initial';
    ev.currentTarget.style.border = 'initial';

    var pParagraph = ev.currentTarget.firstElementChild;
   
    pParagraph.style.visibility = "hidden";

    item = this.item;
    var arrayvalue = item.dataindex;

    if (tempimages.length == 0) {
      rvalue();
      changeImage();
    }
    displayAllImages();
  } else {
    alert("WRONG PLACED");
  }

}
body {
  overflow: hidden;
}

.box {
  width: calc(33.3% - 4px);
  display: inline-block;
  border-radius: 5px;
  border: 2px solid #333;
  border: #000 border-color: #e6e600;
  margin: -2px;
  border-radius: 0%;

}

.box {
  height: 15vh;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  background-size:contain;
}

.box002 {
  position: absolute;
  top: 27.3vh;
  left: 72.98vw;
  cursor: pointer;
}

.box002 img {
  width: 14.0vw;
  height: 23.0vh;
  border-radius: 50%;
}
p{
font: "Courier New", Courier, monospace;
font-size:30px;

 color: rgba(0,0,0,0.6);
text-shadow: 2px 8px 6px rgba(0,0,0,0.2),
                 0px -5px 35px rgba(255,255,255,0.3);

color: #005ce6;
text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container2">
  <div class="containerr">
    <div class="pic" id="content">
      <div id="container">

        <div class="box" ondrop="drop(event)" ondragover="allowDrop(event)" id="10">
          <p name="values"></p>
        </div>
        <div class="box" ondrop="drop(event)" ondragover="allowDrop(event)" id="11">
          <p name="values"></p>
        </div>
        <div class="box" ondrop="drop(event)" ondragover="allowDrop(event)" id="12">
          <p name="values"></p>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="box002" draggable="true" ondragstart="drag(event)" id="2">
  <img src="" draggable="true" id="slide" border="rounded" />
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

删除对象数组Javascript中的重复项

从数组中删除重复项

从JavaScript中的对象数组中删除重复项

SharedPreferences在重复的Android Studio项目中不起作用

JavaScript:删除数组中的重复项

从Swift数组中随机选择一项,无需重复

angularjs项目中的JavaScript .startWith()函数在IE中不起作用

为什么拼接方法不起作用并在TypeScript中删除数组上的项目

为什么我的scss文件中的@use在我的Angular 9.1.0项目中不起作用?

javascript从对象数组中删除重复项

Javascript从对象数组中删除重复项

JavaScript:随机配对数组中的项,而无需重复

验证在MVC4项目中不起作用

从数组角度删除项目不起作用

随机数数组在javascript中不起作用

删除排序的链表代码中的重复项不起作用。这是什么错误?

外键在Rails项目中的activeadmin中不起作用

拼接删除重复的数组值不起作用javascript

按随机选择顺序()查询在 Java 中不起作用

从选择中删除重复项

MySQL:从行中删除重复条目不起作用

DI 在 MVC 项目中的 ViewModel 中不起作用

外部 CSS 在 Python 项目中的 HTML 中不起作用

从项目数组中删除项目不起作用

从 Javascript / Typescript 中的数组中删除子对象重复项

数组中的随机项目,在 Javascript 中不重复元素

从数组中删除对象 id 在 mongoose 中不起作用

使用 Redux 从 Flatlist 中删除项目不起作用

从数组中获取几个随机元素的Javascript代码不起作用

TOP 榜单

热门标签

归档