简体   繁体   English

为什么在我洗牌时我的阵列没有被重新分配?

[英]Why does my array not get reassigned when I shuffle it?

Update After further testing it does appear to work, however the console output is incorrect; 更新进一步测试后,它似乎确实有效,但控制台输出不正确; both arrays still look exactly the same. 两个阵列看起来完全一样。


I am using the Fisher-Yates (Knuth) shuffle as described by this post (code near bottom): http://bost.ocks.org/mike/shuffle/ 我正在使用这篇文章所描述的Fisher-Yates(Knuth)shuffle(底部附近的代码): http//bost.ocks.org/mike/shuffle/

This code correctly shuffles an array of elements. 此代码正确地混洗了一系列元素。 I have tested this in console to verify. 我在控制台中对此进行了测试以验证。 I am working on a project where I need to shuffle an array of arrays. 我正在开发一个项目,我需要对一组数组进行混洗。 This code correctly shuffles the array of arrays, as it is data agnostic. 这段代码正确地改组了数组,因为它与数据无关。

However, when I try to assign the array to the new value, it does not change. 但是,当我尝试将数组分配给新值时,它不会更改。 For example: 例如:

data = [['1','2','3'],['4','5','6'],['7','8','9'],['10','11','12']];
data = shuffle(data); //Value of data does not change, even though shuffle works

When I do 当我做

shuffle(data);

in the console it returns a correctly shuffled array of arrays. 在控制台中,它返回一个正确调整的数组数组。

I have created this JSFiddle so you can see: http://jsfiddle.net/herbstmb/Ve24U/ 我创建了这个JSFiddle所以你可以看到: http//jsfiddle.net/herbstmb/Ve24U/

If you look in your console clearly the shuffle works but the assignment fails. 如果您在控制台中清楚地看到shuffle有效但分配失败。

Many thanks for any help understanding what's going on. 非常感谢任何帮助,了解正在发生的事情。

It does. 确实如此。 Array are mutable. 数组是可变的。 You get the same log in the console because they are both the shuffled result. 您在控制台中获得相同的日志,因为它们都是洗牌结果。 You logged the unshuffled array first, but you inspected it after shuffled, then it is has changed. 您首先记录了未洗涤的阵列,但是在洗牌后检查了它,然后它已经改变了。 Check at the array values in the console and in the source, they differ. 检查控制台和源中的数组值,它们不同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让我的随机播放按钮随机播放数组中的片段? - How do I get my shuffle button to shuffle the pieces in my array? 为什么lodash union会打乱我的对象? - Why does lodash union shuffle my objects? JavaScript - 通过引用传递但在重新分配时丢失的数组 - JavaScript - Array passed by reference but lost when reassigned 如果我在JavaScript中拼接克隆的数组,为什么我的原始数组会被拼接? - Why does my original array get spliced if I splice the cloned array in JavaScript? 为什么此代码可用于我的数组,但不能输入字符串数组呢? 的JavaScript - Why does this code work with my array, but not when I typed in an array of strings? JavaScript 我的随机播放功能由于未知原因而在26点停止。 我怎样才能洗牌? - My shuffle function is stopping at 26 for unknown reasons. How can I get it to shuffle all the cards? 为什么随机播放在React中不起作用? - Why does shuffle not work in React? 为什么当我下载我的数组并尝试获取它的长度时,它只是吐出数组的字节数 - Why is it when I'm downloading my array and try to get the length of it, it just spits out the number of bytes the array is 为什么当我通过 jQuery 将它附加到回调时,我传递给 function 的 functionToCallOnOk 没有被调用? - Why functionToCallOnOk I pass to my function does not get called when I attach it to callback via jQuery? 当我创建两次数组时,为什么我的8-puzzle解决方案运行得更快 - Why does my 8-puzzle solution runs faster when I create an array twice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM