我有这个字符串:
"Test abc test test abc test test test abc test test abc"
Run Code Online (Sandbox Code Playgroud)
干
str = str.replace('abc', '');
Run Code Online (Sandbox Code Playgroud)
似乎只删除abc上面字符串中的第一次出现.如何更换所有出现的内容?
如果我有一个JavaScript对象,请说
const myObject = new Object();
myObject["firstname"] = "Gareth";
myObject["lastname"] = "Simpson";
myObject["age"] = 21;
Run Code Online (Sandbox Code Playgroud)
是否有内置或接受的最佳实践方法来获取此对象的长度?
计算对象的键/属性数的最快方法是什么?它可以在不迭代对象的情况下完成此操作吗?即没有做
var count = 0;
for (k in myobj) if (myobj.hasOwnProperty(k)) count++;
Run Code Online (Sandbox Code Playgroud)
(Firefox确实提供了一个神奇的__count__属性,但是在版本4的某个地方删除了它.)
我注意到,似乎没有明确解释this关键字是什么以及如何在Stack Overflow站点上的JavaScript中正确(和错误地)使用它.
我亲眼目睹了一些非常奇怪的行为,并且无法理解为什么会发生这种行为.
this工作如何以及何时使用?
这是我到目前为止的JavaScript代码:
var linkElement = document.getElementById("BackButton");
var loc_array = document.location.href.split('/');
var newT = document.createTextNode(unescape(capWords(loc_array[loc_array.length-2])));
linkElement.appendChild(newT);
Run Code Online (Sandbox Code Playgroud)
目前,它从URL中获取数组中倒数第二个项目.但是,我想检查数组中的最后一项是"index.html",如果是,请抓住第三项到最后一项.
在JavaScript中替换字符串中所有字符串/字符实例的最快方法是什么?A while,a for-loop,正则表达式?
我正在检查一个变量,例如foo,与多个值的相等性.例如,
if( foo == 1 || foo == 3 || foo == 12 ) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
关键是这个繁琐的任务代码相当多.我想出了以下内容:
if( foo in {1: 1, 3: 1, 12: 1} ) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
但这并不完全吸引我,因为我必须为对象中的项目提供冗余值.
有没有人知道对多个值进行相等检查的正确方法?
我有一个<div>孩子<div>.例如
<div id="niceParent">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
Run Code Online (Sandbox Code Playgroud)
我试图用forEach函数循环它们,因为我认为这document.getElementById("niceParent").children是一个数组,因为我可以访问元素
console.log(document.getElementById("niceParent").children[1]);
console.log(document.getElementById("niceParent").children[2]);
console.log(document.getElementById("niceParent").children[3]);
console.log(document.getElementById("niceParent").children[4]);
Run Code Online (Sandbox Code Playgroud)
因此我试过了
document.getElementById("niceParent").children.forEach(function(entry) {
console.log(entry);
});
Run Code Online (Sandbox Code Playgroud)
这是行不通的.我明白了
TypeError: document.getElementById(...).children.forEach is not a function
Run Code Online (Sandbox Code Playgroud)
作为一种解决方法,我也尝试了一个更复杂的for..in循环:
for (var i in document.getElementById("niceParent").children) {
if (document.getElementById("niceParent").children[i].nodeType == 1) console.log(document.getElementById("niceParent").children[i]);
}
Run Code Online (Sandbox Code Playgroud)
这按预期工作.
为什么?
关于如何将JavaScript 数组拆分为 chunks有一个很好的问题。我目前正在将它用于我正在编写的一些统计方法,我使用的答案如下(尽管我选择不像他们在答案中那样扩展 Array 原型):
var chunk = function(array, chunkSize) {
return [].concat.apply([],
array.map(function(elem,i) {
return i%chunkSize ? [] : [array.slice(i,i+chunkSize)];
})
);
};
Run Code Online (Sandbox Code Playgroud)
这需要一个数组,例如[1,2,3,4,5,6],并给定 a chunkSizeof 2 返回[[1,2],[3,4],[5,6]]。我很好奇如何修改它以创建一组“重叠”块(或者对于那些熟悉诸如移动平均线、“移动子组”等方法的人)。
提供与上述相同的数组和chunkSize3,它将返回[[1,2,3],[2,3,4],[3,4,5],[4,5,6]]. A chunkSizeof 2 将返回[[1,2],[2,3],[3,4],[4,5],[5,6]]。
关于如何解决这个问题的任何想法?
javascript ×9
arrays ×2
replace ×2
string ×2
count ×1
for-loop ×1
foreach ×1
key ×1
parent-child ×1
performance ×1
properties ×1
splice ×1
this ×1