array.toSource() 学习遗传算法中的奇怪数据
我刚刚开始学习遗传算法,我本质上是在写本教程 http://lethain.com/entry/2009/jan/02/genic-algorithms-cool-name-damn-simple/ 到 JavaScript。有一些更改可以更好地代表我的数据集。
不管怎样,当我通过 newPop.toSource() 输出时,我发现
[[#1=[[30,22],#2=[30,85],#3=[30,76]...]]],[#1#,#2#,#3#...#15]]]
我从来没有见过我的 .toSource 输出看起来像这样,我期待的只是一个里面有两个数组的数组
我的代码是
var newPop=populate(data,population,0,70); function individual(population, min, max){ var newIndivids=[]; for(s in population){ newIndivids.push(population[s]); newIndivids[s][0]+=rand; } return newIndivids; } function populate(count,population,min,max){ var popul=[]; for(indiv in count){ popul.push(individual(population,min,max)); } return popul; }
我在哪里做错了什么我的代码给了我这个奇怪的数组结构?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定这些
#1, #2, ...
是什么,但是toSource()
是 gecko 特定的:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/toSource我的猜测是,这是对当时内存中对象的某种“引用”,即不是可移植的输出。
我建议您改用 JSON.stringify,它将输出数据结构的可移植字符串表示形式。
JSON
全局对象将在 Firefox/Safari/Chrome 中开箱即用,但如果您在 IE 中也需要它,您可以在此处获取:http://www.json.org/js.html然后要反转它并取回实际的活动对象,请使用
JSON.parse :
Not sure what those
#1, #2, ...
things are, buttoSource()
is gecko specific: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/toSourceMy guess is that it's some kind of "reference" to the object in memory at that point, i.e. not portable output.
I suggest you use
JSON.stringify
instead, which will output a portable string representation of your data structure.The
JSON
global object will be available in Firefox/Safari/Chrome out of the box, but if you also need it in IE you can get it here: http://www.json.org/js.htmlThen to reverse this and get back an actual living object, use
JSON.parse
: