There is an array (object) that contains positive and negative keys. But trying to sort it out, apparently, he does not perceive negative keys (the search goes from 0) and gives an error. What can be done in this case?

mr = { "0": { title: "qwe" }, "-1": { title: "qwe2" }, "-2": { title: "qwe3" } }; Object.keys(mr) .map(function(file, index) { console.log(mr[index]["title"]); }); 
  • one
    replace index with file - Rostyslav Kuzmovych
  • Please note that your keys are not numbers but strings !!! - ZMS
  • @ZMS, oddly enough, but the keys are always strings - Grundy

1 answer 1

In this case, the values ​​are incorrect: Object.keys(mr) returns an array of keys , that is, in the callback for the map function, the first parameter was called key to reflect the meaning.

 Object.keys(mr) .map(function(key, index) { 

Now it is immediately obvious that index in this case is the key number in the array, and not the key itself.

To get the value from the object, you had to use the key.

 mr = { "0": { title: "qwe" }, "-1": { title: "qwe2" }, "-2": { title: "qwe3" } }; Object.keys(mr) // ["0", "-1", "-2"] .map(function(key, index) { console.log(mr[key]["title"]); }); 

  • one
    On your answers (as the единственного профессионала ) I will study JS - Alexandr_TT