I have simple question. I have JS array like that:
data[-5]=[...];
data[-2]=[...];
data[-1]=[...];
data[4]=[...];
data[6]=[...];
data[7]=[...];
How can I get automatically the minimum (data[-5]) and maximum (data[7]) element?
Negative indexes are not actual indexes. They won't work all time. They are like just as any other property of an object.
However, you could do what you want by iterating through the enumerable properties(Which is bad for an array, but nevertheless) and adding number properties to an Array.
var idxs = Object.keys(arr).filter(isFinite); // just take the Number properties
var min = Math.min.apply(Math, idxs),
max = Math.max.apply(Math, idxs);
Short with no loops:
var keys = Object.keys(data);
var sorted = keys.sort(function(a, b){return a-b});
var min = data[sorted[0]];
var max = data[sorted[sorted.length-1]];
JSON.stringify
you'll lose those, and they won't be reflected in the array length.