简体   繁体   English

JavaScript数组比较(它们不必为===)

[英]JavaScript array comparison(They don't need to be ===)

Long story short, I'm learning js. 长话短说,我正在学习js。 I gave myself a project and I'm stuck. 我给自己一个项目,但我被困住了。 My Project: It's a gambling game where you pick 6 numbers, and they draw 35-48 numbers, faster you get your 6 numbers the larger your winnings, simple. 我的项目:这是一个赌博游戏,您选择6个数字,然后他们抽出35-48个数字,越容易获得越大,您获得的6个数字就越简单。

I'm stuck at arrays, I should have 3 arrays: 我被困在数组上,我应该有3个数组:

  1. 1-48 range 1-48范围
  2. 35 number from 48 thats random generated 从48个多数民众赞成中随机产生35个数字
  3. the 6 numbers that the user inputed. 用户输入的6个数字。

I'm stuck at the 3 array, I don't know what function to use to check i my 2.array has the 6 elements from the 3.array. 我被困在3数组中,我不知道该使用什么函数来检查我的2.array具有3.array中的6个元素。

Here is My code(I did not write the 3.Array becous I don't know where to start): 这是我的代码(我不写3.Array,因为我不知道从哪里开始):

var x = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48'];

var y = x.splice(Math.floor(Math.random() * x.length), 1)[0];


function as() {
    var y = x.splice(Math.floor(Math.random() * x.length), 1)[0];
    document.write(y + '</br>');


}

function Radnom13Brojeva() {
    for (i = 0; i < 13; i++) {
        as();
    };
}

If you need to check if an array contains a certain element, javascript has 2 methods to do so, indexOf() and includes() . 如果您需要检查数组是否包含某个元素,则javascript有2种方法可以做到这一点,即indexOf()includes()

var arr = [1,2,3,4,5,6,7,8,9,10];

//indexOf()
console.log(arr.indexOf(4));   //output: 3
console.log(arr.indexOf("None existing element"));  //output: -1

//includes()
console.log(arr.includes(4));  //output: true
console.log(arr.includes("None existing element"));  //output: false

Keep in mind however that includes() was introduced with EM6 and does not have as extensive a browser support as indexOf(). 但是请记住,includes()是EM6引入的,并没有indexOf()那样广泛的浏览器支持。 The most prominent exclusion being IE. 最突出的排除是IE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM