在第二个周期忽略某些字符串

时间:2016-05-31 16:49:27

标签: javascript arrays cycle

所以,我有一个包含多个字符串的数组。

var Array = ["mods/red1.png", "mods/red2.png", "mods/red3.png", "mods/orange1.png", "mods/orange2.png", "mods/orange3.png", "mods/blue1.png", "mods/blue2.png", "mods/blue3.png"];

我的功能随机选择一个。我需要设置它:如果它拾取了red1,它将忽略所有带红色的字符串,并且只打印出橙色或蓝色中的一个。

当前代码如下所示:

var num = Math.floor(Math.random() * Array.length);
var num2 = Math.floor(Math.random() * Array.length);
     if(RedModArray[num] !== undefined){
                document.mod1.src = Array[num];
if(Array[num].indexOf("red") > -1){
document.mod2.src = Array[num2];
}

但无论我做了什么,它都不会打印出任何内容,或者仍然是随机的。

mod1mod2是html中元素的名称。

2 个答案:

答案 0 :(得分:0)

为你准备了几个笔记;

您只检查两个值。如果Array [num] Array [num2]都包含" red",那么你运气不好。

您将保存到两个不同的位置。根据您的解释,它有时会打印出任何内容",我会猜测您正在打印document.mod2.src,只会填写数组[num]为红色。

要解决这些问题,您需要使用override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? { let superAttributes:NSMutableArray = NSMutableArray(array: super.layoutAttributesForElementsInRect(rect)!) as NSMutableArray let contentOffset = collectionView!.contentOffset let missingSections = NSMutableIndexSet() for layoutAttributes in superAttributes { if (layoutAttributes.representedElementCategory == .Cell) { if let _ = layoutAttributes.indexPath { missingSections.addIndex(layoutAttributes.indexPath.section) } } } for layoutAttributes in superAttributes{ if let representedElementKind = layoutAttributes.representedElementKind { if representedElementKind == UICollectionElementKindSectionHeader { if let indexPath = layoutAttributes.indexPath { missingSections.removeIndex(indexPath.section) } } } } missingSections.enumerateIndexesUsingBlock { idx, stop in let indexPath = NSIndexPath(forItem: 0, inSection: idx) let layoutAttributes = self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader, atIndexPath: indexPath) superAttributes.addObject(layoutAttributes!) } for la in superAttributes { let layoutAttributes = la as! UICollectionViewLayoutAttributes; if let representedElementKind = layoutAttributes.representedElementKind { if representedElementKind == UICollectionElementKindSectionHeader { let section = layoutAttributes.indexPath.section let numberOfItemsInSection = collectionView!.numberOfItemsInSection(section) if numberOfItemsInSection > 0{ let firstCellIndexPath = NSIndexPath(forItem: 0, inSection: section) let lastCellIndexPath = NSIndexPath(forItem: max(0, (numberOfItemsInSection - 1)), inSection: section) var firstCellAttributes:UICollectionViewLayoutAttributes var lastCellAttributes:UICollectionViewLayoutAttributes firstCellAttributes = self.layoutAttributesForItemAtIndexPath(firstCellIndexPath)! lastCellAttributes = self.layoutAttributesForItemAtIndexPath(lastCellIndexPath)! let headerHeight = CGRectGetHeight(layoutAttributes.frame) var origin = layoutAttributes.frame.origin origin.y = min(max(contentOffset.y, (CGRectGetMinY(firstCellAttributes.frame) - headerHeight)), (CGRectGetMaxY(lastCellAttributes.frame) - headerHeight)); layoutAttributes.zIndex = 1024; layoutAttributes.frame = CGRect(origin: origin, size: layoutAttributes.frame.size) } } } } return NSArray(array: superAttributes) as? [UICollectionViewLayoutAttributes] } override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool { return true } 循环,并将最终结果存储到一个位置。

祝你好运!

答案 1 :(得分:0)

您所描述的正是您的代码所做的事情:  *如果"不是红色" =>打印(随机)  *如果"是红色" =>不打印任何东西

我猜你更愿意选择一个后备,如果"是红色"喜欢:  *如果"是红色" =>选一个不同的

var a = ["mods/red1.png", "mods/red2.png", "mods/red3.png", "mods/orange1.png", "mods/orange2.png", "mods/orange3.png", "mods/blue1.png", "mods/blue2.png", "mods/blue3.png"];
var o1 = a[Math.floor(Math.random() * a.length)];
var o2;
do { o2 = a[Math.floor(Math.random() * a.length)]; }
while (o2.indexOf("red") >= 0);

由于random()随机选择,你可能会一遍又一遍地挑选一个红色的。根据您的实际使用情况,甚至可能没有任何有效的元素可供选择。因此,不是上面的解决方案,我宁愿提出这样的事情:在第一次选择后过滤掉你不想要的元素。

var o1 = a[Math.floor(Math.random() * a.length)];
a = a.filter(function(o) { return o.indexOf('red') < 0; })
var o2 = a[Math.floor(Math.random() * a.length)];

PS:不要命名变量&#34; Array&#34;!

相关问题