var items = Array(523,3452,334,31,...5346);
Run Code Online (Sandbox Code Playgroud)
我如何从中获取随机物品items?
我无法绕过这个怪癖.
[1,2,3,4,5,6][1,2,3]; // 4
[1,2,3,4,5,6][1,2]; // 3
Run Code Online (Sandbox Code Playgroud)
我知道[1,2,3] + [1,2] = "1,2,31,2",但我找不到正在执行的类型或操作.
每个JS意见领袖都说扩展原生对象是一种不好的做法.但为什么?我们是否获得了性能?他们是否害怕有人以"错误的方式"做到这一点,并添加了可枚举的类型Object,几乎破坏了任何对象上的所有循环?
以TJ Holowaychuk的should.js为例.他增加了一个简单的getter来Object,一切工作正常(来源).
Object.defineProperty(Object.prototype, 'should', {
set: function(){},
get: function(){
return new Assertion(Object(this).valueOf());
},
configurable: true
});
Run Code Online (Sandbox Code Playgroud)
这真的很有道理.例如,可以扩展Array.
Array.defineProperty(Array.prototype, "remove", {
set: function(){},
get: function(){
return removeArrayElement.bind(this);
}
});
var arr = [0, 1, 2, 3, 4];
arr.remove(3);
Run Code Online (Sandbox Code Playgroud)
是否有任何反对扩展本机类型的论据?
我已经读过全局变量都不好的地方,应该使用替代方案.在Javascript中,我应该选择什么解决方案.
我正在考虑一个函数,当有两个arguments(function globalVariables(Variable,Value))时,看看Variable是否存在于一个本地数组中,并且它是否将它的值设置为Value,else,Variable并被Value追加.如果在没有arguments(function globalVariables())的情况下调用该函数,则返回该数组.也许如果仅使用一个参数(function globalVariables(Variable))触发该函数,它将返回Variable数组中的值.
你怎么看?我想听听你使用全局变量的替代解决方案和论据.
globalVariables();function append(){
globalVariables("variable1","value1"); //globalVariables() would append variable1 to it's local array.
};
function retrieve(){
var localVariable1 = globalVariables("variable1"); //globalVariables() would return "value1".
};
function retrieveAll(){
var localVariable1 = globalVariables(); //globalVariables() would return the globalVariable()'s entire, local [persistently stored between calls] array.
};
function set(){
globalVariables("variable1","value2"); //globalVariables() would set variable1 to "value2".
};
Run Code Online (Sandbox Code Playgroud)
这是Singleton Pattern BTW吗? …
可能重复:
JavaScript:从数组中获取随机值
var numbers = new Array('1','2','4','5','6','7','8','9','10');
Run Code Online (Sandbox Code Playgroud)
我有一个JavaScript数组,现在想从中随机选择四个不同的数字,然后在页面上表达(通过document.write).显然,每次用户重新加载页面时,它将显示四个不同的随机数.
可能重复:
JavaScript:从数组中获取随机值
有人可以帮我解决这个话题吗?我有这个代码.
var textArray = [
'song1.ogg',
'song2.ogg'
]
audioElement.setAttribute('src', textArray);
Run Code Online (Sandbox Code Playgroud)
如何随机将其中一个字符串添加到我的音频元素中?
如果有人可以帮助,我会很高兴....
我正在阅读初学者的JavaScript书籍,其中包含一些代码,用于将编码器的输入(var answer)与从数组中随机选择的字符串(答案)进行比较.这是一个猜谜游戏.
我对随机选择字符串的方式感到困惑.代码似乎是将Math.random函数乘以answers数组及其length属性.检查周围,这似乎是从数组中随机选择的标准方法?你为什么要使用数学运算符*来乘以......一个基于数组长度的随机字符串?技术上长度不是只有3个字符串吗?我觉得它应该像index = answers.random一样简单.JS或其他语言中是否存在这种情况?
<script>
var guess = "red";
var answer = null;
var answers = [ "red",
"green",
"blue"];
var index = Math.floor(Math.random() * answers.length);
if (guess == answers[index]) {
answer = "Yes! I was thinking " + answers[index];
} else {
answer = "No. I was thinking " + answers[index];
}
alert(answer);
</script>
Run Code Online (Sandbox Code Playgroud) 我试图围绕d3的包布局(http://bl.ocks.org/4063530).
我有基本的布局工作,但我想用新数据更新它.即收集新数据,将其绑定到当前layout.pack并相应更新(更新/退出/输入).
我的尝试在这里(http://jsfiddle.net/emepyc/n4xk8/14/):
var bPack = function(vis) {
var pack = d3.layout.pack()
.size([400,400])
.value(function(d) {return d.time});
var node = vis.data([data])
.selectAll("g.node")
.data(pack.nodes)
.enter()
.append("g")
.attr("class", function(d) { return d.children ? "node" : "leaf node"; })
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
node.append("circle")
.attr("r", function(d) { return d.r });
node.filter(function(d) { return !d.children; }).append("text")
.attr("text-anchor", "middle")
.attr("dy", ".3em")
.text(function(d) { return d.analysis_id });
bPack.update = function(new_data) {
console.log("UPDATE");
node …Run Code Online (Sandbox Code Playgroud) 可能重复:
JavaScript:从数组中获取随机值
我有一个带有以下行的外部js:
var postmessage = "hi my favorite site is http://google.com";
Run Code Online (Sandbox Code Playgroud)
但有没有办法从阵列中随机选择一个网站,就像这样
var postmessage = "hi my favorite site is +'random'";
random= http://google.com, http://yahoo.com, http://msn.com, http://apple.com
Run Code Online (Sandbox Code Playgroud)
我如何使它工作?
我有一个 MongoDB 文档集合,它有两个属性:类型和值。
[
{type: "A", value: "1"},
{type: "A", value: "2"},
{type: "B", value: "1"},
{type: "B", value: "2"},
{type: "C", value: "1"},
{type: "C", value: "2"}
]
Run Code Online (Sandbox Code Playgroud)
如何在不涉及任何 JavaScript 的情况下使用单个查询获取每种类型的随机文档?
我试图用聚合框架找出一些东西
db.collection.aggregate([
{$group: {_id: "$type", item: {$push: "$$ROOT"}}},
{$sample: {size: 1}}
]);
Run Code Online (Sandbox Code Playgroud)
它不会对每组应用抽样,而只是选择其中一个组。
javascript ×9
arrays ×2
random ×2
circle-pack ×1
d3.js ×1
data-binding ×1
jquery ×1
mongodb ×1
prototype ×1
transition ×1