Monday, July 30, 2012

Turn a fake array created by me into a real array in JavaScript



I know that in JavaScript sometimes the system creates a fake array, meaning it is actually an object and not an instance of Array, but still has part of the functionality of an array. For example, the arguments variable you get inside functions is a fake array created by the system. In this case I know that to turn it into a real array you can do:



var realArray = Array.prototype.slice.call(fakeArray);


But what if the fake array wasn\’t created by the system, what if fakeArray was simply:



var fakeArray = { \"0\": \"some value\", \"1\": \"another value\" };


In this case, and I tested it, using the method above will result in an empty array. The thing I want to be able to turn a fake array like in the example I gave (created by me and not by the system) into a real array. And before you tell me to simply make the fake array a real array from the beginning, you should know that I get the fake array from a resource which I have no control of.


So, how do I turn a fake array not created by the system into a real array?






Rating: 1 out of 5 based on 3 ratings



The post Turn a fake array created by me into a real array in JavaScript appeared first on Javascript ASK.






via Javascript ASK http://javascriptask.phpfogapp.com/turn-a-fake-array-created-by-me-into-a-real-array-in-javascript.html

No comments:

Post a Comment