gpt4 book ai didi

javascript - 随机选择没有重复项的数组项而不删除项(JavaScript)

转载 作者:行者123 更新时间:2023-11-30 17:03:53 26 4
gpt4 key购买 nike

我见过很多关于随机选择数组项而不重复的问题。但是,其中大多数是通过使用拼接方法来回答的。但这会删除项目。

我已经随机选择了我的项目,但它们在重复。在我的两个函数中,我从每个随机选择的项目中选择两个“子项目”。这两个功能不能一起工作,我正在寻找一种方法,可以选择两个不同的随机选择的项目,而不会重复,也不会删除它们。有人能帮帮我吗?

(使用 Adob​​e Edge Animate)

var xml_source = "series.xml";
var initLoadScript = false;

var items = [];
var itemTitle1;
var obj = new Object();
var previousNumber = -1;

loadXML();

function loadXML() {
$.ajax({
type: "GET",
url: xml_source,
dataType: "xml",
success: function(xml) {

$(xml).find('sbs').find('channel').find('item').each(function() {
items.push($(this));
});

itemOne();
itemTwo();
}
});
}

function itemOne(){
var randomNumber = Math.floor(Math.random()*14);
var assignItem = randomNumber;
console.log("random nummer 1: " + assignItem);
sym.$("TitleText1").html(items[assignItem].find("author_name").text());
sym.$("Image1").html("<img src='"+items[assignItem].find('media\\:content, content').find('media\\:thumbnail, thumbnail').attr('url')+"' width='145'/>");
}

function itemTwo(){
var randomNumber = Math.floor(Math.random()*14);
var assignItem = randomNumber;
console.log("random nummer 2: " + assignItem);
sym.$("TitleText2").html(items[assignItem].find("author_name").text());
sym.$("Image2").html("<img src='"+items[assignItem].find('media\\:content, content').find('media\\:thumbnail, thumbnail').attr('url')+"' width='145'/>");
}

XML 结构示例:

<?xml version="1.0" encoding="UTF-8" ?>
<sbs version="1.0" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<title>Feed</title>
<description>Video</description>
<link>//www.URL.com</link>
<lastBuildDate>Fri</lastBuildDate>
<pubDate>Fri</pubDate>
<ttl>1</ttl>


<item>
<title>title</title>
<description>aflevering</description>
<link>//www.google.com</link>
<guid>//www.google.com</guid>
<formatname>berg</formatname>
<pubDate>Wed</pubDate>

<!-- oEmbed -->
<oembed>
<version>1.0</version>
<type>video</type>
<provider_name>provider</provider_name>
<provider_url>http://www.video.nl/url</provider_url>
<width>10</width>
<height>10</height>
<title>title1</title>
<author_name>author name</author_name>
<author_url>http://www.google.com/</author_url>
</oembed>

<media:content
url="http://google.com"
type="text/html"
medium="document"
expression="full"
height="10"
width="10"
lang="us">
<media:title type="plain">title</media:title>
<media:description type="plain">title descr</media:description>
<media:thumbnail url="http://google.com/tiger.jpg" width="10" height="10" />
</media:content>

</item>

最佳答案

我不太了解您要实现的目标,但这是我一次获得随机元素的一种方式

var letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N"];
var getRandom = (function (array) {
var notGivenItems = array.map(function (el) {return el;}),
var getIndex = function () {
return Math.floor(Math.random() * notGivenItems.length);
};

return function () {
if (notGivenItems.length === 0) {
return;
}

return notGivenItems.splice(getIndex(), 1)[0];
};
})(letters); // items, in your case

getRandom(); // some letter
getRandom(); // some other letter
...
getRandom(); // different letters until all are given

// if the method is called more times than the array length it'll return undefined

编辑:由于@JLRishe 评论提高了性能

关于javascript - 随机选择没有重复项的数组项而不删除项(JavaScript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28384459/

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