How to access index from a javascript array

Asked

Viewed 14,567 times

3

In giving console.log in 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 must access this array through the value of your keys (36,37,97,98,99,100,101) and that can 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]);
})

5 answers

5


you can check the keys using a conventional for. Follow 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: http://jsfiddle.net/g82aac1d/

5

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

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

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

But if you want to treat your array as an array, without negative indexes, and only refer to the index ignoring the blank entries, I suggest using the $.each even testing the value per undefined (or null and undefined, or any value falsy, depending on your objective):

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>
<div id="saida"></div>

3

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

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

var chaves = Object.keys(meuObjeto);

If you want to iterate all properties of that object you can use one 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
  • May be a sparse array, see mgibsonbr’s response

1

You could use the method indexOf that serves to locate through the value assigned to the index, as in the example below:

var produtosRestaurante = array()

 produtosRestaurante[0] = 'Cachorro Quente' 
 produtosRestaurante[1] = 'X-Burger'
 produtosRestaurante[2] = 'Suco Natural'
 produtosRestaurante[3] = 'Refrigerante'

produtosRestaurante.indexOf(X-Burger) What would be recovered by using this method would be the index whose value was assigned (X-Burger in this case), ie the recovered Dice is the number 1

0

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.