How to access index of a javascript array

2

When I give console.log to the variable overlays , I have the following return:

[Oh, 36: Oh, 37: Oh, 97: Oh, 98: Oh, 99: Oh, 100: Oh, 101: Oh]
0: Oh
36: Oh
37: Oh
97: Oh
98: Oh
99: Oh
100: Oh
101: Oh
length: 102
__proto__: Array[0]

I should access this array through the value of your keys (36,37,97,98,99,100,101) and may be different.

When trying to access them with the method below, the value of the keys comes only as (0,1,2,3,4,5,6)

$.each(overlays, function(a, b) {
    console.log(overlays[a]);
})
    
asked by anonymous 11.02.2015 / 14:41

4 answers

4

You can ask for the keys using a conventional for. Here is an example:

var array = { 0: 'Oh', 36: 'Oh', 37: 'Oh', 97: 'Oh', 98: 'Oh', 99: 'Oh', 100: 'Oh', 101: 'Oh' }

for (var key in array) {
    console.log({
        key: key,
        value: array[key]
    });    
}

JSFIDDLE: link

    
11.02.2015 / 14:49
4

If an array (and not just an object) has non-sequential numeric properties, the "size" of that array (i.e. its length property) is one more than its largest nonnegative index. If your array does not have elements with a non-negative index, its length is zero.

The method jQuery.each , when applied to arrays, traverses all "elements of that array" in order sequential. This means that any "missing" element will come as undefined . Other properties, including negative indexes, will not be returned by this method.

The solution therefore depends on your goal: if your array is actually an object with arbitrary properties, and you want to get all of them, you can use for..in as suggested in TobyMosque's answer or Object.keys as suggested in the response of Sergio . Taking care to ignore length , of course, if that is in your interest.

But if you want to treat your array as an array even with no indexes, and only query the index by ignoring the blank entries, I suggest using $.each even testing the value by undefined (or null and undefined , or any false value, depending on your purpose):

var overlays = ['Oh'];
overlays[36] = 'Oh';
overlays[37] = 'Oh';
overlays[97] = 'Oh';
overlays[98] = 'Oh';
overlays[99] = 'Oh';
overlays[100] = 'Oh';
overlays[101] = 'Oh';

$.each(overlays, function(a, b) {
    if ( b !== undefined ) {
        document.querySelector("#saida").innerHTML += "<p>" +
            "Índice: " + a + "; Elemento: " + b +
        "</p>";
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="saida"></div>
    
11.02.2015 / 16:21
2

This array that you put [Oh, 36: Oh, 37: Oh, 97: Oh, 98: Oh, 99: Oh, 100: Oh, 101: Oh] is not an array ... looks more like an object. Anyway to have keys and values then it is Object you need.

To extract all the keys from the object in an array you can do:

var chaves = Object.keys(meuObjeto);

If you want to iterate all the properties of this object you can use for var in but the fastest is using Object.keys:

var chaves = Object.keys(meuObjeto);
for (var i = 0; i < chaves.length; i++){
    var propriedade = meuObjeto[chaves[i]];
}

If you know the key / property whose value you want to change you can use two ways:

meuObjeto['chave']
// ou
meuObjeto.chave
    
11.02.2015 / 15:01
0

The method jQuery.inArray(<chave>, <array>); returns the index of the element if it is found in the last array, if it does not find it it will return -1;

    
10.04.2017 / 21:09