【问题标题】:I JSON.stringify an object but how do I print out specific elements of object in Javascript我 JSON.stringify 一个对象,但是如何在 Javascript 中打印出对象的特定元素
【发布时间】:2018-02-21 11:44:39
【问题描述】:

新的 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: Cannot convert undefined or null to 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) 

【问题讨论】:

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


【解决方案1】:

您需要将对象转换为数组 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)

【讨论】:

  • 它说Cannot convert undefined or null to object at Function.keys ()
猜你喜欢
  • 1970-01-01
  • 2020-09-27
  • 1970-01-01
  • 2013-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-26
  • 2021-10-09
相关资源
最近更新 更多