There is such an array in JS:

var files = []; 

It has one element:

 [1430904228000: "b3ce520734e536cb9a6ecf8665a6f479"] 

If you do this:

 console.log(files); // Выводит элемент с ключом 1430904228000 if(files.length > 0){ alert('OK'); // Не выводит сообщение } 

Full code:

 var files: set: function (key, hash) { files[key] = hash[0]; }, ... 

    3 answers 3

    Generally

     function countProperties(obj) { var count = 0; for(var prop in obj) { if(obj.hasOwnProperty(prop)) ++count; } return count; } if(countProperties(files) > 0){ alert('OK'); 

    And this is like a homework

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Indexed_collections
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

    • All code is as follows: updated question - Jony
    • You again show some stub. But I have a suspicion that the files in the files are a dictionary and not an array. And you need to count the number of keys. Canonical variant function function countProperties (obj) {var count = 0; for (var prop in obj) {if (obj.hasOwnProperty (prop)) ++ count; } return count; } Modern version: Object.keys ({"1": "2"}). Length - VladimirAbramov
    • This is not the js syntax, this is console.log output. - Duck Learns to Take Cover
    • Despite the fact that the solution may work, the answer @MakarovAV is more correct and explains the reason for the strange behavior. - VladimirAbramov

    I realized you looked at what is contained in the array via console.log. Console.log automatically brought the string index to the number, that is, it shows not what it is.

    1430904228000 is more than (2 32 - 2) = 4,294,967,294 - the maximum numerical index of the array in javascript. This magic number exists because the maximum length of the array is defined in the spec as the maximum 32-bit unsigned integer, and the indices start at 0.

    Arrays in js are inherited from objects, and according to an array, you can specify any string key and its value as any object, but changing the array string keys does not affect its length property, which you can easily verify in the console:

     var arr=[0]; arr["1"]=1; //Строковые индексы которые можно привести к числу, к числу приведутся arr.smth = 'trololo'; arr["1430904228000"] = 'b3ce520734e536cb9a6ecf8665a6f479'; console.log(arr.smth); console.log(arr.length); console.log(arr); // И здесь мы увидим тот самый "странный" элемент массива который на самом деле строковый. 

    It seems that this construction is perceived by the interpreter as a string key of the object "1430904228000", therefore the length does not change.

    It is worth adding that adding string keys to an array is a bad practice, because firstly it destroys interpreter optimizations, secondly, other programmers usually don’t expect them there.

    • Very efficient answer! Tell me how to add an element to the array, specifying a string key? So: file.{key} = value ? - Jony
    • @Jony, do not add string keys to the array! It destroys interpreter optimization and finally can be a bad practice - Duck Learns to Take Cover
    • How to be then? Replace with an object? - Jony
    • @Jony, either replace with an object or take care of assigning normal numeric keys that will be inserted into a range of numeric values ​​normally perceived by arrays - Duck Learns to Hide

    In the array, the elements are separated, for example:

     [1, 'str', obj] 

    What you presented is more like an object, an example of an object:

     var obj = {prop : value} 
    • But after all adding happens as in an array: files[key] = hash[0]; - Jony
    • @Jony, so you can also add to the object, try - Duck Learns to Take Cover