我看到很多关于随机选择数组项没有重复的问题。然而,他们中的大多数是通过拼接方法来回答的。但是这会删除项目。随机选择数组项没有删除项目没有重复项(JavaScript)
我已经随机选择了我的物品,但它们正在重复。在我的两个功能中,我从每个随机选择的项目中选择两个“子项目”。这两个函数不能一起工作,我正在寻找一种方法,可以选择两个不同的随机选择的项目,而不必重复,也不需要删除它们。有人可以帮我吗?
(使用Adobe Edge Animate中)
var xml_source = "series.xml";
var initLoadScript = false;
var items = [];
var itemTitle1;
var obj = new Object();
var previousNumber = -1;
loadXML();
function loadXML() {
\t $.ajax({
\t \t type: "GET",
\t \t url: xml_source,
\t \t dataType: "xml",
\t \t success: function(xml) {
\t \t \t $(xml).find('sbs').find('channel').find('item').each(function() {
\t \t \t \t items.push($(this));
\t \t \t });
\t \t \t itemOne();
\t \t \t itemTwo();
\t \t }
\t });
}
function itemOne(){
\t var randomNumber = Math.floor(Math.random()*14);
\t var assignItem = randomNumber;
\t console.log("random nummer 1: " + assignItem);
\t sym.$("TitleText1").html(items[assignItem].find("author_name").text());
\t sym.$("Image1").html("<img src='"+items[assignItem].find('media\\:content, content').find('media\\:thumbnail, thumbnail').attr('url')+"' width='145'/>");
}
function itemTwo(){
\t var randomNumber = Math.floor(Math.random()*14);
\t var assignItem = randomNumber;
\t console.log("random nummer 2: " + assignItem);
\t sym.$("TitleText2").html(items[assignItem].find("author_name").text());
\t sym.$("Image2").html("<img src='"+items[assignItem].find('media\\:content, content').find('media\\:thumbnail, thumbnail').attr('url')+"' width='145'/>"); \t
}
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>
您将有两个数组:一个是原始的,而另一个是重复的。随意移除副本中的随机项目,这样可以确保它们不会重复。 – mattsven 2015-02-07 16:49:48
可以发布'series.xml'吗? – guest271314 2015-02-07 17:44:14
@ guest271314由于项目的隐私原因,我无法共享完整的XML。但在这种情况下,我只需要XML文件的和节点。 –
2015-02-07 18:49:28