Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Foreach when key is not a number

In my project i push a array into a table in firebase realtime database. Firebase generate a token and not a numeric id for each array:

{
    "-N2mToYDj-i8ToErmaUj": {
        "anzahl": 2,
        "groesse": 0.5,
        "name": "getraenk1",
        "preis": 5.5
    },
    "-N2mX3RPnDXxWMHMJScy": {
        "anzahl": 1,
        "groesse": "0.25",
        "name": "getraenk2",
        "preis": 2.2
    },
    "-N2mXBT8c7EKlIgyrU72": {
        "anzahl": 1,
        "groesse": "0.5",
        "name": "getraenk3",
        "preis": 3.4
    },
    "-N2mXZD1BoCslj81Lcya": {
        "anzahl": 1,
        "groesse": "1",
        "name": "getraenk4",
        "preis": 5.2
    "-N2m_g8GutpAFqsN4WsP": {
        "anzahl": 1,
        "groesse": "0.33",
        "name": "getraenk5",
        "preis": 3.2
    },
}

How can i work with each object when the key value is not numeric. ForEach() does not work for me.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Lets say your data stored in test object.
you can use Object function in javascript to work with any object. For accessing all key you can try this:

Object.keys(test).map(key => { // key in here can be for example -N2mToYDj-i8ToErmaUj
    const data = test[key] // this is the full object that each key points to
/* 
   {
        "anzahl": 2,
        "groesse": 0.5,
        "name": "getraenk1",
        "preis": 5.5
    }
*/
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading