+ 1

Does anybody understand why I can't sort my array into a new array despite using JSON.parse(JSON.stringify(array))?

https://code.sololearn.com/W798n590sOm3/?ref=app

30th Apr 2019, 6:58 AM
Andreas Schenk
Andreas Schenk - avatar
5 odpowiedzi
+ 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
30th Apr 2019, 10:21 AM
Gordon
Gordon - avatar
+ 4
To copy an array you can use `arr.slice()`.
30th Apr 2019, 9:27 AM
Schindlabua
Schindlabua - avatar
+ 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.
30th Apr 2019, 4:48 PM
Schindlabua
Schindlabua - avatar
+ 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.
30th Apr 2019, 4:41 PM
Andreas Schenk
Andreas Schenk - avatar
+ 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.
30th Apr 2019, 5:08 PM
Andreas Schenk
Andreas Schenk - avatar