找出JavaScript数组是否包含对象的最简洁有效的方法是什么?
这是我知道的唯一方法:
function contains(a, obj) {
for (var i = 0; i < a.length; i++) {
if (a[i] === obj) {
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
有没有更好,更简洁的方法来实现这一目标?
这与Stack Overflow问题密切相关问题在JavaScript数组中查找项目的最佳方法是什么?它使用的方法寻找数组中的对象indexOf.
javascript arrays algorithm time-complexity javascript-objects
我有一个非常奇怪的问题...在每个浏览器和移动版本中我遇到这种行为:
怎么能避免这个问题?当我第一次听到视口高度时,我很兴奋,我认为我可以将它用于固定高度块而不是使用javascript,但现在我认为唯一的方法是实际上javascript与一些resize事件......
任何人都可以帮我/建议一个CSS解决方案吗?
简单的测试代码:
/* maybe i can track the issue whe it occours... */
$(function(){
var resized = -1;
$(window).resize(function(){
$('#currenth').val( $('.vhbox').eq(1).height() );
if (++resized) $('#currenth').css('background:#00c');
})
.resize();
})Run Code Online (Sandbox Code Playgroud)
*{ margin:0; padding:0; }
/*
this is the box which should keep constant the height...
min-height to allow content to be taller than viewport if too much text
*/
.vhbox{
min-height:100vh;
position:relative;
}
.vhbox .t{
display:table;
position:relative;
width:100%;
height:100vh;
}
.vhbox .c{ …Run Code Online (Sandbox Code Playgroud)我需要javascript将5添加到整数变量,但是它将变量视为字符串,因此它写出变量,然后在"字符串"的末尾添加5.我怎么能强迫它做数学呢?
var dots = document.getElementById("txt").value; // 5
function increase(){
dots = dots + 5;
}
Run Code Online (Sandbox Code Playgroud)
输出: 55
我怎么强迫它输出10?
可能是我的脚本中的错误吗?
我正在这样初始化55:
var dots = document.getElementById("txt").value; // 5
function increase(){
dots = dots + 5;
}
Run Code Online (Sandbox Code Playgroud) 当我尝试使用传播运算符有条件地合并两个对象时,当条件为true或时,它将起作用false:
let condition = false;
let obj1 = { key1: 'value1'}
let obj2 = {
key2: 'value2',
...(condition && obj1),
};
// obj2 = {key2: 'value2'};
Run Code Online (Sandbox Code Playgroud)
当我尝试对数组使用相同的逻辑时,它仅在条件为时才起作用true:
let condition = true;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition && arr1)];
// arr2 = ['value2', 'value1']
Run Code Online (Sandbox Code Playgroud)
如果条件为false错误,则抛出:
let condition = false;
let obj1 = { key1: 'value1'}
let obj2 = {
key2: 'value2',
...(condition && obj1),
};
// obj2 = {key2: …Run Code Online (Sandbox Code Playgroud)在下面的正则表达式中,\s表示空格字符.我想象正则表达式解析器,正在通过字符串看到\并知道下一个字符是特殊的.
但事实并非如此,因为需要双重逃脱.
为什么是这样?
var res = new RegExp('(\\s|^)' + foo).test(moo);
Run Code Online (Sandbox Code Playgroud)
是否有一个具体的例子说明单个逃避如何被误解为其他东西?
这可能是非常愚蠢的事情,但我不明白为什么这不起作用.
var a = {"cat":"large"};
a.forEach(function(value, key, map){
console.log(value);
});
Run Code Online (Sandbox Code Playgroud)
未捕获的TypeError:a.forEach不是函数
我尝试使用/不同的关键字和运算符时如何解释,发现以下语法完全合法:
// awaiting something that isn't a Promise is fine, it's just strange to do:
const foo = await /barbaz/
myFn()Run Code Online (Sandbox Code Playgroud)
错误:
未捕获的ReferenceError:未定义等待
看起来它尝试将解析await为变量名 ..?我期待
等待仅在异步功能中有效
或类似的东西
意外的令牌等待
令我恐惧的是,您甚至可以为其分配以下内容:
const await = 'Wait, this actually works?';
console.log(await);Run Code Online (Sandbox Code Playgroud)
如若不是如此明显的错误原因语法错误,因为它确实有let,finally,break,等?为什么允许这样做?第一个代码片段到底发生了什么?
javascript specifications language-design async-await es2017
在使用isNumeric函数时,我发现了这种边缘情况:
[5]被认为是一个数,可以用数字运算符等中使用 +,-,/等等,并给出5时给予parseFloat。
JavaScript为什么将单个值数组转换为数字?
例如
const x = [10];
console.log(x - 5, typeof x);Run Code Online (Sandbox Code Playgroud)
给
5 object
Run Code Online (Sandbox Code Playgroud) 使用new RegExp("regex");和/same_regex/测试目标字符串之间有什么区别吗?我问这个问题,因为我在使用这两种方法时得到了不同的验证结果.以下是我用于验证电子邮件字段的代码段:
var email="didxga@gmail.comblah@foo.com";
var regex1 = new RegExp("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$");
var regex2 = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
//using RegExp object
if(regex1.test(email)) {
console.log("email matched regex1");
} else {
console.log("email mismatched regex1");
}
//using slash notation
if(regex2.test(email)) {
console.log("email matched regex2");
} else {
console.log("email mismatched regex2");
}
我得到了两个不一致的结果:
email matched regex1 email mismatched regex2
我想知道这里是否有任何差异,或者我在这个具体例子中省略了什么?
有关可执行示例,请参阅此处
var a;
if (true) {
a = 5;
function a() {}
a = 0;
console.log(a)
}
console.log(a)Run Code Online (Sandbox Code Playgroud)
我看到了上面的代码,在{}中声明了一个函数。我认为它会打印0 0,但它会打印0 5