Javascript中的"for ... in"循环是否按照声明的顺序循环遍历hashtables/elements?是否有一个浏览器没有按顺序执行?
我想要使用的对象将被声明一次,永远不会被修改.
假设我有:
var myObject = { A: "Hello", B: "World" };
Run Code Online (Sandbox Code Playgroud)
我进一步使用它们:
for (var item in myObject) alert(item + " : " + myObject[item]);
Run Code Online (Sandbox Code Playgroud)
在大多数体面的浏览器中,我可以期待'A:'你好''总是先来'B:'世界"吗?
我试图在javascript中随机生成十六进制的颜色.
然而,产生的颜色几乎与彼此无法区分.
有没有办法改善它?
这是我正在使用的代码:
function randomColor(){
var allowed = "ABCDEF0123456789", S = "#";
while(S.length < 7){
S += allowed.charAt(Math.floor((Math.random()*16)+1));
}
return S;
}
Run Code Online (Sandbox Code Playgroud)
我听说过有关HSL和HSV颜色模型的一些内容,但无法在我的代码中使用它.请帮忙.
提前致谢
有人知道ECMAScript5的Object.keys()在常见实现中的时间复杂度吗?它是O(n)
用于n
钥匙?假设哈希实现,时间是否与哈希表的大小成比例?
我正在寻找语言实现者或某些现实世界基准测试的保证.
我有一个数组数组.
var ArrOfarr = { A1: ["choice", "choice2", "choice3"], A2: ["choice1", "choice2"], A3: ["choice1", "choice2"], A4: [], A5: [], A6: [], A7: [] }
Run Code Online (Sandbox Code Playgroud)
我想在每次单击按钮时从'ArrOfarr'中选择随机数组.我试过下面的内容,但看到'undefined':
function A()
{
var item = ArrOfarr[Math.floor(Math.random()*ArrOfarr.length)];
alert(item);
}
Run Code Online (Sandbox Code Playgroud)
如何从上面的数组中获取随机数组(不重复直到达到它的长度).我怎样才能得到随机挑选的数组的名称?
我有以下代码:
$.getJSON('js/questions1.json').done(function(data){
window.questionnaire = data;
console.log(window.questionnaire);
startGame();
});
Run Code Online (Sandbox Code Playgroud)
这会从服务器中带来一个json并将其记录到变量中.在此之后,我想选择questions.json文档中的随机问题:
function pickRandomQuestion(){
window.selectedquestion = window.questionnaire[Math.floor(Math.random * window.questionnaire.length)];
console.log(window.selectedquestion);
console.log(window.questionnaire);
}
Run Code Online (Sandbox Code Playgroud)
但是,当console.log()
selectquestion变量没有返回时,它是未定义的.我的代码有问题吗?我已经三倍检查它,我看到它没什么不好,但它可能只是我和我一起玩游戏.
以下是json的外观:
"q1" : {
"question" : "This country is one of the largest wine-producing countries of the world, where wine is grown in every region of the country. Which country is this?",
"a" : "France",
"b" : "Italy",
"c" : "Germany",
"d" : "Australia",
"corrrect" : "b"
},
"q2" : {
"question" : "What is the name for …
Run Code Online (Sandbox Code Playgroud) 在Javascript中给出类似字典的对象,例如{a:1, b:-2, c:42}
,是否有一种随机选择属性的简单方法?
在上面的例子中,我想有,将返回的函数a
,b
或c
随机.
我提出的解决方案如下:
var proplist = []
forEach(property in foo) {
if(propertyIsEnumerable(foo[property]) {
proplist.push(property);
}
}
var n = proplist.length;
// randomly choose property (randInt(n) returns a random integer in [0,n))
proplist[randInt(n)];
Run Code Online (Sandbox Code Playgroud)
有没有比较惯用的方法呢?
可能的重复:
从 Javascript 对象中选择随机属性
假设我有一个 javascript 对象:
Config.UI.Area = {
"uuid": {
"_type": "string"
},
"frame": {
"_type": "rect"
},
"zIndex": {
"_type": "string"
}
}
Run Code Online (Sandbox Code Playgroud)
然后我想通过使用 for 循环获取每个属性的名称,例如“uuid”、“frame”、“zIndex”:
var prop=config.ui.area;
var arr=new Array()
for(var i in prop){
//do something with prop[i]?
//arr.push(prop[i].....)?
}
Run Code Online (Sandbox Code Playgroud)
我不能直接使用 prop[i],它会返回一个对象,我怎么能得到它的名字?
我指的是这个问题:Pick random property from a Javascript object
在标记的答案中,作者使用了以下代码:
if (Math.random() < 1/++count)
result = prop;
Run Code Online (Sandbox Code Playgroud)
我的问题:
谢谢!
function randomKey(obj) {
var ret;
var c = 0;
for (var key in obj)
if (Math.random() < 1/++c)
ret = key;
return ret;
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下这个代码如何公平地从对象中选择一个随机密钥?
javascript ×9
jquery ×2
random ×2
arrays ×1
ecmascript-5 ×1
for-loop ×1
json ×1
object ×1
performance ×1
properties ×1