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?
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?
Share Improve this question asked Jul 30, 2012 at 13:59 CokegodCokegod 8,43410 gold badges31 silver badges47 bronze badges 4-
1
Have you tried to add a
length
property to the object? I believe almost allArray.*
modifications use the array's length. – Zeta Commented Jul 30, 2012 at 14:01 - 2 @Elias Van Ootegem You obviously didn't read my entire question. – Cokegod Commented Jul 30, 2012 at 14:03
- @EliasVanOotegem Why not read the question before asking things that are answered in it? – Anthony Grist Commented Jul 30, 2012 at 14:04
- Sorry, didn't read the last sentence – Elias Van Ootegem Commented Jul 30, 2012 at 14:09
3 Answers
Reset to default 5Your example will work if your "fake array" is given a .length
property appropriately set.
This will not work in some older versions of Internet Explorer.
"one of the purposes of turning the fake array into a real array is to get its length"
If you want the number of properties in the object, use Object.keys
...
var len = Object.keys(fakeArray).length;
To shim Object.keys
for older browsers, you can do this...
if (!Object.keys) {
Object.keys = function(o) {
var keys = [];
for (var k in o)
if (o.hasOwnProperty(k))
keys.push(k)
return keys;
};
}
If the fake Array is "sparse", you'll need a solution like @Rocket shows.
You can just simply loop through the "array" and save the values into a real array.
var fakeArray = { "0": "some value", "1": "another value" };
var realArray = [];
for(var i in fakeArray){
realArray[i] = fakeArray[i];
}
You can iterate the object properties, while pushing the ones you want to your new array:
var array = [];
for (var i in fakeArray) if (fakeArray.hasOwnProperty(i)) {
array.push(fakeArray[i]);
}