如何比较数组中的元素(javascript)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20020551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare elements in an array (javascript)
提问by user2984273
Apologies if this sounds very basic but I'm a total novice when it comes to coding (I'm trying my best to get better though!)
抱歉,如果这听起来很基本,但在编码方面我完全是新手(尽管我正在尽力变得更好!)
I'm making a simple higher or lower card game with javascript in which the user guesses if the next card will be higher or lower than the current card shown.
我正在用 javascript 制作一个简单的更高或更低的纸牌游戏,其中用户猜测下一张牌是高于还是低于显示的当前牌。
I currently have an array of 52 cards & I'm using Math.random to randomly generate a card from the array.
我目前有一个包含 52 张卡片的数组,我正在使用 Math.random 从数组中随机生成一张卡片。
What I want is to compare the value of two cards but I'm not sure how to go about it. I've got a feeling it might involve IndexOf but I'm not sure.
我想要的是比较两张卡的价值,但我不知道如何去做。我有一种感觉,它可能涉及 IndexOf,但我不确定。
Thanks a million & I'm sorry if this is off topic or a duplicate!
感谢一百万,如果这是题外话或重复,我很抱歉!
Here's my array if it helps.
如果有帮助,这是我的数组。
function randomCard()
{
var cardArray = ["Club0.jpg","Club1.jpg","Club3.jpg","Club3.jpg","Club4.jpg","Club5.jpg","Club6.jpg","Club7.jpg","Club8.jpg","Club9.jpg","Club10.jpg","Club11.jpg","Club12.jpg","Diamond0.jpg","Diamond1.jpg","Diamond2.jpg","Diamond3.jpg","Diamond4.jpg","Diamond5.jpg","Diamond6.jpg","Diamond7.jpg","Diamond8.jpg","Diamond9.jpg","Diamond10.jpg","Diamond11.jpg","Diamond12.jpg","Heart0.jpg","Heart1.jpg","Heart2.jpg","Heart3.jpg","Heart4.jpg","Heart5.jpg","Heart6.jpg","Heart7.jpg","Heart8.jpg","Heart9.jpg","Heart10.jpg","Heart11.jpg","Heart12.jpg","Spade0.jpg","Spade1.jpg","Spade2.jpg","Spade3.jpg","Spade4.jpg","Spade5.jpg","Spade6.jpg","Spade7.jpg","Spade8.jpg","Spade9.jpg","Spade10.jpg","Spade11.jpg","Spade12.jpg"];
var pickCard = cardArray[Math.floor(Math.random() * cardArray.length)];
document.getElementById("card").src = pickCard;
}
回答by Andy
I think the problem here is not that you don't have an understanding of arrays and looping, but how to frame your question properly before you start. At present you have the following:
我认为这里的问题不在于您不了解数组和循环,而在于如何在开始之前正确地构建您的问题。目前你有以下几点:
var cardArray = ["Club0.jpg","Club1.jpg","Club3.jpg"...];
And it you're making your life much more difficult for yourself because performing a numerical comparison on these array elements - while not impossible - would involve regex test that you're probably not quite ready for yet.
并且您让自己的生活变得更加困难,因为对这些数组元素进行数值比较 - 虽然并非不可能 - 将涉及您可能还没有完全准备好的正则表达式测试。
If instead you said "I have 4 suits of cards, and the cards have a value of 1 - 13 (11 being a Hyman up to 13 a King, Aces low), then you can start to understand the problem scope better.
相反,如果您说“我有 4 套牌,并且这些牌的点数为 1 - 13(11 是一个Hyman,13 是一个国王,A 低),那么您可以开始更好地理解问题范围。
Let's start by creating the deck - four suits with 14 cards each.
让我们从创建套牌开始——四套花色,每套 14 张牌。
var deck = {
heart: [1,2,3,4,5,6,7,8,9,10,11,12,13],
club: [1,2,3,4,5,6,7,8,9,10,11,12,13],
spade: [1,2,3,4,5,6,7,8,9,10,11,12,13],
diamond: [1,2,3,4,5,6,7,8,9,10,11,12,13]
};
We need to hold the names of the suits in a separate array so we can access the object.
我们需要将西装的名称保存在一个单独的数组中,以便我们可以访问该对象。
var suits = ['heart','club','spade','diamond'];
Next, memo
is important. Since we don't want to take an identical card on each draw we need to make a note of what cards have already been taken; memo
is a store of chosen cards which we reference.
接下来,memo
很重要。因为我们不想在每次抽奖时都拿一张相同的牌,所以我们需要记下已经拿过的牌;memo
是我们参考的所选卡片的存储。
var memo = [];
Now for the function drawRandomCard
. It takes a random suit from our suit array, and a number from the array of that chosen suit. If the card is in memo
we draw again otherwise we add it to memo
and return the card. However, because we still want to run a comparison on the values, we're going to return an array, the first element is the suit, the second the value.
现在的功能drawRandomCard
。它从我们的花色数组中随机抽取一个花色,并从所选花色的数组中获取一个数字。如果卡片在memo
我们再次抽牌,否则我们将其添加到memo
卡片中并返回卡片。但是,因为我们仍然想对值进行比较,所以我们将返回一个数组,第一个元素是西装,第二个元素是值。
function drawRandomCard() {
var suit = suits[Math.floor(Math.random() * suits.length)];
var number = deck[suit][Math.floor(Math.random() * 13)];
if (memo[suit + number]) drawRandomCard();
memo.push(suit + number)
return [suit, number];
}
For example:
例如:
var card1 = drawRandomCard();
var card2 = drawRandomCard();
To test the values, compare the second values of the array against each other:
要测试这些值,请将数组的第二个值相互比较:
console.log(card1[1] > card2[1]); // true or false
Then, should you want to, you can match the value of those variables to whatever images you need to load using join
to merge the card array elements together.
然后,如果您愿意,您可以将这些变量的值与您需要加载的任何图像进行匹配,join
以将卡片数组元素合并在一起。
var img = new Image();
img.src = card1.join('') + '.jpg'; // eg. diamond4.jpg
Or:
或者:
document.getElementById("card").src = card1.join('') + '.jpg';
回答by Oriol
I would use something like:
我会使用类似的东西:
function Card(suit, number) {
this.suit = suit;
this.number = number;
}
Card.prototype.image = function() {
return this.suit + this.number + '.jpg';
};
Card.compare = function(a, b) {
/* Define how you want to compare cards. My guess is: */
return a.number < b.number;
};
Card.prototype.compareTo = function(other) {
return Card.compare(this, other);
};
var suits = ['Club', 'Diamond', 'Heart', 'Spade'],
cardArray = [];
for (var i = 0; i < suits.length; ++i) {
for (var j = 0; j <= 12; ++j) cardArray.push(new Card(suits[i], j));
}
var currentCard = cardArray[0]; /* Or whatever initial card */
function randomCard() {
var pickCard = cardArray[Math.random() * cardArray.length | 0];
if(currentCard.compareTo(pickCard)) {
/* Do something */
} else {
/* Do something else */
}
document.getElementById("card").src = pickCard.image();
currentCard = pickCard;
}
If you want that once a card has been picked, it can't be picked again (like @MackieeE said), replace
如果你想要一张牌一旦被选中,就不能再被选中(就像@MackieeE 说的),替换
var pickCard = cardArray[Math.random() * cardArray.length | 0];
with
和
var pickCard = cardArray.splice(Math.random() * cardArray.length | 0, 1)[0];
in order to remove picked card from cardArray
.
为了从cardArray
.
回答by bitoffdev
This will get the value out of the card's image string if card="Club10.jpg"
:
如果出现以下情况,这将从卡片的图像字符串中获取值card="Club10.jpg"
:
card.match(/\d+/g)
Will return:
将返回:
10
For example:
例如:
var cardArray = ["Club0.jpg","Club1.jpg","Club3.jpg","Club3.jpg","Club4.jpg","Club5.jpg","Club6.jpg","Club7.jpg","Club8.jpg","Club9.jpg","Club10.jpg","Club11.jpg","Club12.jpg","Diamond0.jpg","Diamond1.jpg","Diamond2.jpg","Diamond3.jpg","Diamond4.jpg","Diamond5.jpg","Diamond6.jpg","Diamond7.jpg","Diamond8.jpg","Diamond9.jpg","Diamond10.jpg","Diamond11.jpg","Diamond12.jpg","Heart0.jpg","Heart1.jpg","Heart2.jpg","Heart3.jpg","Heart4.jpg","Heart5.jpg","Heart6.jpg","Heart7.jpg","Heart8.jpg","Heart9.jpg","Heart10.jpg","Heart11.jpg","Heart12.jpg","Spade0.jpg","Spade1.jpg","Spade2.jpg","Spade3.jpg","Spade4.jpg","Spade5.jpg","Spade6.jpg","Spade7.jpg","Spade8.jpg","Spade9.jpg","Spade10.jpg","Spade11.jpg","Spade12.jpg"];
var pickCard1 = cardArray[Math.floor(Math.random() * cardArray.length)];
var pickCard2 = cardArray[Math.floor(Math.random() * cardArray.length)];
if(parseInt(pickCard1.match(/\d+/g)) === parseInt(pickCard2.match(/\d+/g))){
/*Do something here if the first equals the second card*/
} else if(parseInt(pickCard1.match(/\d+/g)) > parseInt(pickCard2.match(/\d+/g))){
/*Do something here if the first card is greater than the second card*/
}else{
/*Do something here if the first card is less than the second card*/
}