我JSON.stringify一个对象,但如何在Javascript

时间:2018-02-21 11:44:40

标签: javascript firebase ionic-framework firebase-realtime-database

javascript及以下代码的新功能我正在尝试在对象中打印出某些内容。

           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));

下面是控制台中一个对象的示例,该对象使用上面的代码进行打印,但如何在没有括号的情况下打印出日期,描述,位置,名称和价格。我尝试过list.eventList.date但是说日期未定义。我也尝试过console.log(list.eventList [0] .date)但返回的内容无法读取属性' 0'未定义的     在snapshot.forEach

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

以下是我的firebase数据库的一部分。

  {
  "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 ]
            },

当我在console.log(JSON.stringify(list,null,2));我得到了一切,但我只想打印每个用户的每个事件列表

        {
    "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"
}

我尝试将对象转换为数组,但是说ERROR TypeError:无法将undefined或null转换为object     在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) 

1 个答案:

答案 0 :(得分:0)

您需要将对象转换为数组Object并访问它。



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)