Hello!
Faced a problem, I can't normally parse json into javascript .
Here is json :

 { "Suicide Squard": { "small-link": "https://www.reg.ru/i/svg/b-header__logo_site_ru.svg", "title": "Suicide Squad", "gif": "https://vk.com/images/icons/head_icons.png" }, "Sticker Shop": { "small-link": "https://www.reg.ru/i/svg/b-header__logo_site_ru.svg", "title": "Sticker Shop", "gif": "https://vk.com/images/icons/head_icons.png" } } 

It is necessary that in the end could do something like this:

 parsed[1].small-link 
  • I do not fully understand why you want to parse an object into an array. Well, the parsed ["Suicide Squard"] will be out of the box. Or do you have somewhere in the code clearly stated that "Suicide Squard" is 1? - kpower

1 answer 1

Learn the basics of JS: JSON.parse .

 let parsed = JSON.parse`{ "Suicide Squard": { "small-link": "https://www.reg.ru/i/svg/b-header__logo_site_ru.svg", "title": "Suicide Squad", "gif": "https://vk.com/images/icons/head_icons.png" }, "Sticker Shop": { "small-link": "https://www.reg.ru/i/svg/b-header__logo_site_ru.svg", "title": "Sticker Shop", "gif": "https://vk.com/images/icons/head_icons.png" } }`; console.info(parsed['Suicide Squard']['small-link']); 

But this is an object, there are not keys, but keys. In this case - the name.
Further, data.small-link cannot be data.small-link - not a valid identifier, use the bracket notation as in the example.

  • Thank you! Another question appeared, for example project = 3 I call: console.log (database [project] ['small-link']); TypeError: Cannot read property 'small-link' of undefined - Kelotixy
  • one
    @Kelotixy, Thanks here it’s customary to express in the pros. Of course, I already know what a project and what it is about, I just pretend I don’t know :) And the error says that it cannot take the small-link property because the database[project] did not find anything ( undefined returned) . - user207618