我试图理解'空'稀疏数组(例如new Array(3))和等效'空'密集数组(具有3个未定义条目的数组)之间的区别.
我可以用这两种方式创建一个包含3个未定义值的数组:
var sparse = new Array(3);
// or
var sparse = [,,,];
var dense = Array.apply(null, Array(3)); // See dense array link below
Run Code Online (Sandbox Code Playgroud)
如果我为其中任何一个执行console.log,结果是:
[undefined, undefined, undefined]
Run Code Online (Sandbox Code Playgroud)
如果我遍历每个数组以将其与另一个数组进行比较,它们将严格匹配:
console.log(sparse.length === dense.length);
// true
for (var i = 0; i < dense.length; i++) {
console.log(i +':'+ (dense[i] === sparse[i]));
}
// '0:true'
// '1:true'
// '2:true'
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用.forEach(或map,reduce等),那么回调将永远不会在稀疏数组上调用,但会在密集数组上调用三次:
sparse.forEach(function(val,i){
console.log(i +':'+ val);
});
// Nothing. No-op.
dense.forEach(function(val,i){
console.log(i +':'+ val);
}); …Run Code Online (Sandbox Code Playgroud) 由于限制,我必须在我的网站的每个页眉上设置一个URL字符串作为这样的JavaScript字符串变量 var burl = "http://www.example.com";
现在,我必须将此字符串传递到我网站burl的HTML href=""标记内.我还希望能够在burl旁边的href链接中添加额外的URL元素.
这是完整的代码看起来像Javascript + HTML代码;
<script>
var burl = "http://www.example.com";
</script>Run Code Online (Sandbox Code Playgroud)
<html>
<head>
</head>
<body>
<h1>JavaScript Variables</h1>
<p>the href below should link to http://www.example.com</p>
<a href="{burl-string-here}/blog/article/">Open Here</a>
</body>
</html>Run Code Online (Sandbox Code Playgroud)
任何帮助解决这个简单的问题将不胜感激.
我编写了一个非常慢的函数来生成从AA000到ZZ999的代码(按顺序而非随机).我已经得出结论,必须有一个更好的方法来做到这一点.有关如何加快速度的任何建议吗?
function generateAlphaNumeric(){
theAlphabet = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
resultArrray = [];
resultArrray2 = [];
teller = 0;
for(i in theAlphabet){
for(x in theAlphabet){
resultArrray[teller] = theAlphabet[i] + theAlphabet[x];
teller++;
}
}
teller = 0;
for(x = 0; x<10; x++){
for(y = 0; y<10; y++){
for(z = 0; z<10; z++){
resultArrray2[teller] = x.toString() + y.toString() +z.toString();
teller++;
}
}
}
teller = 0;
finalArray = [];
for(index in resultArrray){
for(i in resultArrray2){
finalArray[teller] = resultArrray[index] + resultArrray2[i];
teller++;
}
}
//console.log(resultArrray);
//console.log(resultArrray2);
console.log(finalArray); …Run Code Online (Sandbox Code Playgroud) 我有这样的html标签:
<il class="currItem" data-sourcemp3="http://**">
<il class="currItem" data-sourcemp4="http://**">
Run Code Online (Sandbox Code Playgroud)
我想得到这个数据值,即使这个mp3或mp4我写道:
var A = $('.currItem').attr("data-source(.+)")
console.log(A);
Run Code Online (Sandbox Code Playgroud)
要么:
var srcName = new RegExp(/data-source.+/g);
var A = $('.currItem').attr(srcName)
console.log(A);
Run Code Online (Sandbox Code Playgroud)
我得到"未定义"
我是怎么做到的 提前致谢
我在PHP中填充关联数组,并在JS函数中访问该数组。我使用json_encode()将PHP数组转换为JS数组。我使用IE 8运行此应用程序。在使用IE 8的某些计算机中,for(;;)可以工作,但在其他计算机中则失败。在某些使用IE 8的计算机中,for(var in)可以工作,但在其他计算机中则失败。以下代码有什么区别?
for (var k = 0; k < ruleList.length; k++){ //do something }
for (var x in ruleList){ //do something }
Run Code Online (Sandbox Code Playgroud) 我的 react-app 组件中有两个函数
componentDidMount() {
if(Object.keys(this.props.praticiens).length>0)
Object.keys(this.props.praticiens).map((praticien) => {
if(this.props.praticiens[praticien].identifiant_user === getUrlParams(window.location.hash).identifiant_user)
this.setState({
praticien:this.props.praticiens[praticien].id_user
})
})
}
handleChangePraticien = (praticien) => {
this.setState({ praticien }, () => {
Object.keys(this.props.praticiens).map((praticien) => {
if(this.state.praticien === this.props.praticiens[praticien].id_user)
this.props.selectPraticienFunction(this.props.praticiens[praticien].identifiant_user);
})
})
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到:
Line 26:64: Expected to return a value in arrow function array-callback-return
Line 36:64: Expected to return a value in arrow function array-callback-return
Run Code Online (Sandbox Code Playgroud)
例如第 26Object.keys(this.props.praticiens).map((praticien) => {行在 componentDidMount上开始,第 36 行在 handleChangePraticien 函数上是同一行
我该如何解决?
我有一个带有一些键和值的Javascript对象:
var obj = {
"key1" : "val1",
"key2" : "val2",
"key3" : "val3",
"key4" : ""
}
Run Code Online (Sandbox Code Playgroud)
我想迭代所有键并检索所有值.
我试过两种方法:
1)使用for(键中的var键)
var keys = Object.keys(obj);
for (var key in keys) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
这个解决方案的问题是key对象是一个数组,所以我必须使用obj [keys [key]]].不是很漂亮
此外,检查"key4"时,返回值为"0"而不是""(空).
2)使用forEach
Object.keys(obj).forEach(function(key){
// ...
});
Run Code Online (Sandbox Code Playgroud)
这种情况下的问题是,如果我尝试这样做:
Object.keys(obj).forEach(function(key){
obj[key]; // <- obj is undefined !!
});
Run Code Online (Sandbox Code Playgroud)
"obj"变量在foreach中未定义!
迭代所有键以检索所有值的最佳方法是什么?
谢谢
我正在分析一些第三方JavaScript库,并遇到了一种方法,人们可以快速参考核心原型.这样做有什么性能上的好处吗?任何人都能用一个例子来解释这个吗
var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;
// Create quick reference variables for speed access to core prototypes.
var
push = ArrayProto.push,
slice = ArrayProto.slice,
concat = ArrayProto.concat,
toString = ObjProto.toString,
hasOwnProperty = ObjProto.hasOwnProperty;
Run Code Online (Sandbox Code Playgroud) 我有一个数组包含
const data = ['a', 'b', 'c', 'd'];
Run Code Online (Sandbox Code Playgroud)
如何找到最后一个元素,结果应该是'd'
var a = "foo";
var c = Array.prototype.join.call( a, "-" ); // 'f-o-o'
Run Code Online (Sandbox Code Playgroud)
第二行代码如何工作?我没有看到任何字符串转换为数组然后再转换回来,这是在后台发生的吗?我遇到过这种代码,这很奇怪,一个接受字符串的数组方法.