假设我有一个这样的循环:
for (var i = 0; i < SomeArrayOfObject.length; i++) {
if (SomeArray[i].SomeValue === SomeCondition) {
var SomeVar = SomeArray[i].SomeProperty;
return SomeVar;
}
}
Run Code Online (Sandbox Code Playgroud)
快速问题:是否return停止循环执行本身?
除了提高可读性之外,还有什么优势可以includes结束indexOf?他们看起来和我一模一样.
这有什么区别
var x = [1,2,3].indexOf(1) > -1; //true
Run Code Online (Sandbox Code Playgroud)
还有这个?
var y = [1,2,3].includes(1); //true
Run Code Online (Sandbox Code Playgroud) 我正在寻找一个好的算法来获取一个数组中不是另一个数组中的元素的所有元素.所以给定这些数组:
var x = ["a","b","c","t"];
var ?????????y = [???????"d","a","t","e","g"];
Run Code Online (Sandbox Code Playgroud)
我想最终得到这个数组:
var z = ["d","e","g"];
Run Code Online (Sandbox Code Playgroud)
我正在使用jquery,所以我可以利用$.each()和$.inArray().这是我提出的解决方案,但似乎应该有更好的方法.
// goal is to get rid of values in y if they exist in x
var x = ["a","b","c","t"];
var y = ["d","a","t","e","g"];
var z = [];
$.each(y, function(idx, value){
if ($.inArray(value,x) == -1) {
z.push(value);
}
});
?alert(z); // should be ["d","e","g"]
Run Code Online (Sandbox Code Playgroud)
这是代码的实际应用.有任何想法吗?
我对这个任务有点困惑我有这样一个数组:array = ["123","456","#123"]我想找到包含#的元素.我用过array.includes("#"),array.indexOf("#")但它没有工作任何想法?
如何Array.includes()在if语句中测试多个.include ?以下似乎适用于一个项目,即。 slug-title-one但忽略第二个。
if (item.slug.includes('slug-title-one' || 'slug-title-13')) {
// checks for and displays if either
}
Run Code Online (Sandbox Code Playgroud) 我想用 JQuery 检查两个数组是否至少有一个公共元素并返回 true。以下行不能按我想要的方式工作:
if(jQuery.inArray(array1,array2) > -1) {return true;}
Run Code Online (Sandbox Code Playgroud)
您能帮我找到解决方案吗?
Javascript不允许使用includes?比较数组
请参阅以下示例:
let x = [[1,2], [3,4]]
let y = x[0]
let z = [1,2]
console.log(y,z)
// Output: [ 1, 2 ] [ 1, 2 ]
console.log(x.includes(y), x.includes(z))
// Output: true false
Run Code Online (Sandbox Code Playgroud)
我想一定x.includes(z)要true。
注释使我意识到了qurestion 检查是否包含一个数组的任何元素,但它不能回答我的问题,因为我想检查数组是否具有完全相同的元素includes不仅使用了一些。
而且,这个问题如何在javascript中比较数组并不能解释为什么includes不起作用。它说明了如何做,这不是我的问题的重点。
我有两个数组
var master= ["1","2","3"];
var arr = ["1","5"];
Run Code Online (Sandbox Code Playgroud)
我想检查是否arr包含中的任何项目master。根据这里的SO post,我有以下仅适用于chrome的代码
var found = arr.some(r => master.indexOf(r) >= 0);
Run Code Online (Sandbox Code Playgroud)
但是它不适用于IE11。IE11引发错误
第23行的https:// localhost:44328 / js / xxxx.js \ n \ nSCRIPT1002中的JavaScript严重错误 :语法错误
我也尝试过
var found = arr.some(r => master.includes(r) >= 0);
Run Code Online (Sandbox Code Playgroud) 我有两个简单的数组:
var arr_must_exist = ["1","2","3"];
var arr_created = ["2","4","7","8"]; //This is varies depend of the array created.
Run Code Online (Sandbox Code Playgroud)
所以,如果arr_created像示例那样应该有一个警告Your choice must contain 1 and 3
例:
arr_created = ["2","4","7","8"];
alert(`Your choice must contain 1 and 3`)
arr_created = ["2","3","7","8"];
alert(`Your choice must contain 1`)
arr_created = ["4","5","7","8"];
alert(`Your choice must contain 1, 2 and 3`)
arr_created = ["1","2","9","3"];
alert(`Your choice is valid`)
Run Code Online (Sandbox Code Playgroud)
我用,$.each但它只检查数组.不比较它们.
$.each(arr_is_must_exist , function(index, val_must_exist) {
$.each(arr_is_exist_in_table , function(index, val_is_exist) {
if(val_must_exist != val_is_exist){ …Run Code Online (Sandbox Code Playgroud) const colors = ["black", "red", "pink", ];
Run Code Online (Sandbox Code Playgroud)
这是colors数组。我可以检查颜色数组中是否存在其中一个值。例如,在这里,我想检查颜色数组中是否存在红色。我使用下面的代码。
Const IsRedExist = colors.includes("red"); //returns true
Run Code Online (Sandbox Code Playgroud)
所以我想要一个标志来知道颜色数组中是否存在红色、白色或蓝色。我该如何实现这一目标?对此有何建议?
将是一个非常简单的问题,
我有这两个数组:
var pcNumbers = [1,2,3,4,5];
var userNumbers = [1,2,7,8,9];
Run Code Online (Sandbox Code Playgroud)
实际上,我必须找到一种方法来创建一个警报,提示“有两个共同元素,1 和 2”
蒂亚
我需要一个函数来触发该元素是否recordplayerstick包含pinplace或pinsongplay类。我当前拥有的代码返回语法错误。正确的方法是什么?
if (document.getElementById('recordplayerstick').classList.contains('pinplace pinsongplay')) {
removepin();
}
Run Code Online (Sandbox Code Playgroud)