Roo*_*Jay 6 javascript node.js node-inspector console.log visual-studio-code
我想在source arry和sort arry之间添加"After sort:",如下所示:
但控制台偶尔会显示:
为什么for-of-loop还没有运行完毕,跳出来运行console.log?源代码:
// Random to generate double digits.
function getRandom() {
return Math.round(Math.random() * 100);
}
// Writing data to the array.
var score = [
["a", getRandom()],
["b", getRandom()],
["c", getRandom()],
["d", getRandom()],
["e", getRandom()]
];
console.log("Before sort:");
// Print source arry
for (let m of score) {
console.log(m);
}
// Call sort()
score.sort((a, b) => {
return b[1] - a[1];
});
console.log("After sort:");
// Print sort arry.
for (let n of score) {
console.log(n);
}
Run Code Online (Sandbox Code Playgroud)
Raj*_*med -1
当您尝试打印时,由于多次从数组属性访问相同的方法,并且该方法包含一些数值计算,因此数组似乎没有准备好,因此被超时包围
// Random to generate double digits.
function getRandom() {
return Math.round(Math.random() * 100);
}
// Writing data to the array.
var score = [
["a", getRandom()],
["b", getRandom()],
["c", getRandom()],
["d", getRandom()],
["e", getRandom()]
];
console.log("Before sort:");
setTimeout(function(){
// Print source array
for (let m in score) {
console.log(m);
}
// Call sort()
score.sort((a, b) => {
return b[1] - a[1];
});
console.log("After sort:");
for (let n in score) {
console.log(n);
}
},300);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
206 次 |
最近记录: |