gpt4 book ai didi

javascript - 从数组中删除元素并将其移动到另一个

转载 作者:行者123 更新时间:2023-11-28 03:07:02 25 4
gpt4 key购买 nike

我必须随机生成测验,答案是 3 个随机名称,其中 1 个是正确的,我已经完成了随机化和洗牌,但我必须在显示的元素出现时将其从数组中取出,以便它当我单击下一步时,它不会被随机化并再次选择,当我单击下一步时,它会显示下一个随机化的东西,但目前它不会切换它。有人可以帮忙吗?

var receivedArray   = JSON.parse(0: {nameIMGTOP: "-5", nameIMGTOPafter: "-10", nameIMGLEFT: "18", nameIMGLEFTafter: "22", nameTOP: "-3"}
1: {nameIMGTOP: "13", nameIMGTOPafter: "11", nameIMGLEFT: "27", nameIMGLEFTafter: "11", nameTOP: "32"}
2: {nameIMGTOP: "-6", nameIMGTOPafter: "", nameIMGLEFT: "36", nameIMGLEFTafter: "", nameTOP: "-3"}
3: {nameIMGTOP: "16", nameIMGTOPafter: "", nameIMGLEFT: "55", nameIMGLEFTafter: "", nameTOP: "18"}
4: {nameIMGTOP: "13", nameIMGTOPafter: "", nameIMGLEFT: "73", nameIMGLEFTafter: "", nameTOP: "37"});
console.log(receivedArray);
const StartElements = receivedArray;
const correctAnswer = StartElements.splice(Math.floor(Math.random() * StartElements.length), 1)[0];
const guess1 = StartElements.splice(Math.floor(Math.random() * StartElements.length), 1)[0];
const guess2 = StartElements.splice(Math.floor(Math.random() * StartElements.length), 1)[0];
//array with few elements and variables for randomizing different things from the array

function move() {
array = new Array([]);
while(correctAnswer !== 1){
correctAnswer.splice();
array.push(correctAnswer);
console.log(array);
}
}
//I've tried many different things and I can't get it down

//Everything displays in Jquery made HTML
$("#test-div").append(
"<div class=\"row\">\n"+
"<div class=\"col-6\">\n" +
"<div class=\"ImageDiv\">" +
"<img id=\"testImage\" src=\"\" alt='...' align=\"middle\"/>\n" +
"</div>" +
"</div>\n" +
"<h4 class=\"Guess\" id=\"Guess\">ATMINI JŪRNIEKU MEZGLA NOSAUKUMU</h4>\n"+
"<p id=\"description\"></p>"+
"<div class=\"col-6 shuffle\">\n" +
"<div class=\"btn guesses\" >"+correctAnswer.nameLV+"</div><br>"+
"<div class=\"btn guesses\" >"+guess1.nameLV+"</div><br>"+
"<div class=\"btn guesses\" >"+guess2.nameLV+"</div><br>"+
"</div>\n" +
"</div>\n"
);

最佳答案

正如我在评论中所说,很难弄清楚你想要做什么,但如果这只是“删除特定项目”的问题,那么使用 Array.filter是一种很好且简单的方法:

const startArray = [{0: 'item 0'}, {1: 'item 1'}, {2: 'item 2'}, {3: 'item 3'}]

const elementToRemove = startArray[1] // or whichever item you want

const newArray = startArray.filter(item => item !== elementToRemove)

console.log(newArray)

// => [{0: 'item 0'}, {2: 'item 2'}, {2: 'item 3'}]

关于javascript - 从数组中删除元素并将其移动到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60525209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com