简体   繁体   中英

I JSON.stringify an object but how do I print out specific elements of object in Javascript

New to javascript and below code I am trying print out certain things in an object.

           firebase.database().ref('/userProfile/').orderByChild('eventList').once('value', (snapshot) => {
  this.users = [];
  snapshot.forEach((childSnapshot) => {
    var list = childSnapshot.val();
    console.log(JSON.stringify(list.eventList, null, 2));

below is an example of one objects in the console that is printing with above code but how do I print out just the date, description ,location, name and price without the parenthesis. I tried doing list.eventList.date but says date undefined. I also tried console.log(list.eventList[0].date) but that returned Cannot read property '0' of undefined at snapshot.forEach

             {
    "-L5niHTgOezEJWI8ge6O": {
    "date": "2018-02-20",
      "description": "lijliil",
        "location": "lijli",
          "name": "jiji",
       "numOfTickets": "9",
        "price": "7",
     "tickets": [
             0,
             1,
             2,
             3,
             ]
            }
           }

Below is part of my firebase database.

  {
  "userProfile" : {
"3RQOUe9cqcRX2IMy0S290S9trSM2" : {
  "birthDate" : "2018-02-20",
  "email" : "acs@acs.com",
  "firstName" : "asda",
  "lastName" : "dsad",
  "userType" : "customer"
},
"9VvVRfTt7sPayXuifhBYrincYs12" : {
  "birthDate" : "2018-02-20",
  "email" : "testmail@email.com",
  "eventList" : {
    "-L5nEpqLyOO5hid53lf8" : {
      "date" : "2018-08-16",
      "description" : "drake ",
      "location" : "02 dublin",
      "name" : "drake",
      "numOfTickets" : "10",
      "price" : "80",
      "tickets" : [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
            },

when I console.log(JSON.stringify(list, null, 2)); I get everything but I just want print out each eventLists for every user

        {
    "birthDate": "2018-02-20",
    "email": "liam@user1.com",
    "eventList": {
        "-L5nUpmVU3NUgeUWYvcN": {
            "date": "2018-02-20",
            "description": "ijdljdlijf",
            "location": "dublin",
            "name": "liam",
            "numOfTickets": "12",
            "price": "123",
            "tickets": [
                0,
                1,
                2,
                3,
                4,
                5,
                6,
                7,
                8,
                9,
                10,
                11
            ]
        },
        "-L5nWt640bwKAGwZ-rwZ": {
            "date": "2018-02-20",
            "description": "uhkuh",
            "location": "khuuh",
            "name": "xsacs",
            "numOfTickets": "4",
            "price": "12",
            "tickets": [
                0,
                1,
                2,
                3
            ]
        }
    },
    "firstName": "liam",
    "lastName": "mcaweeney",
    "userType": "eventorganiser"
}

I tried turning object into array but says ERROR TypeError: Cannot convert undefined or null to object at Function.keys ()

               snapshot.forEach((childSnapshot) => {
    var data = childSnapshot.val();
    var eventList = data.eventList;

    const eventListArray = Object.keys(eventList).map(i => eventList[i])

    console.log(eventListArray[0].date) 

You need to convert your object to array Object and access it.

 var data = { "birthDate": "2018-02-20", "email": "liam@user1.com", "eventList": { "-L5nUpmVU3NUgeUWYvcN": { "date": "2018-02-20", "description": "ijdljdlijf", "location": "dublin", "name": "liam", "numOfTickets": "12", "price": "123", "tickets": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ] }, "-L5nWt640bwKAGwZ-rwZ": { "date": "2018-02-20", "description": "uhkuh", "location": "khuuh", "name": "xsacs", "numOfTickets": "4", "price": "12", "tickets": [ 0, 1, 2, 3 ] } }, "firstName": "liam", "lastName": "mcaweeney", "userType": "eventorganiser" } var eventList = data.eventList; const eventListArray = Object.keys(eventList).map(i => eventList[i]) console.log(eventListArray[0].date)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM