The output you've shown doesn't match either of the loops you've shown, but the gist seems to be that you don't want to see the undefined array entries.
I understand why it happens, but I want use exactly jQuery $.each for my script.
If you really want to do that, it's easy enough:
$.each(d, function(i, v){
if (d.hasOwnProperty(i)) {
console.log(i);
console.log(v);
}
});
That will weed out the array indexes that don't exist in the array. You could use typeof v[i] !== 'undefined' but that will give you the incorrect result if you've actually set an array element to undefined (so it exists, but its value is undefined).
jQuery is iterating over the array in the normal way, starting with index 0 and going through to index length - 1. The for..in loop loops over the properties in the object, and so doesn't loop for non-existant array entries (but will, the way you've written it, pick up anything from Array.prototype that's enumerable; more here).
Ways to loop things:
$.each; see above.
Specifically for arrays and array-like things, the staid but useful for (index = 0; index < v.length; ++index). In this case, because the array is sparse, you'll want v.hasOwnProperty(i) or typeof v[i] !== 'undefined' (if you don't care about the case I listed above) to make sure you're seeing an entry that exists.
For objects in general (including arrays, but you have to be careful): for..in:
for (propName in v) {
// Check whether `v` has the property itself, or inherited it
if (v.hasOwnProperty(propName)) {
// `v` has the property itself
// If looping an array or array-like thing and you want to see
// only the *elements* (properties with numeric indexes) and
// skip any non-element properties:
if (String(parseInt(propName, 10)) === propName) {
// It's an array element
}
}
}
forloop which won't have those undefineds. I assume you mean you're seeing those in the$.eachloop.