+ 1
Does anybody understand why I can't sort my array into a new array despite using JSON.parse(JSON.stringify(array))?
5 Réponses
+ 2
JSON is for processing JSON format, why do you use it on array?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
You can either change your data into JSON format, or change to use Array method
https://code.sololearn.com/W3cNMx37R657/?ref=app
+ 4
To copy an array you can use `arr.slice()`.
+ 3
I haven't looked at the code in detail, but if your specimen have a very complex genome, maybe it is worth spending a few minutes to build a `Genome` class, and give that a `clone` method which does a proper deep copy.
That way at least the complexity is in one place. Also it will be a lot more performant! That JSON thing is rather hacky.
+ 1
Thank you. At first I acctually used the array method, but I had to learn the hard way arr.slice returns only a shallow copy of an array. Since my arrays are multidimensional I only get a copy by reference on the lower levels. To avoid that we have to use either a very complex encapsulation of arr.slice or turn the whole array to a string and parse it back with JSON.
+ 1
If you think a 'Genome' class would help I have to look into that. Unfortunatly I'm not yet very good in thinking in oop. Procedural programming seems more natural to me. I just hoped since arrays are like opjects in Javascript I would be coverd.