[英]How to get property out of JavaScript object which is only appearing in console.log() but not in JSON.stringify()?
[英]I JSON.stringify an object but how do I print out specific elements of object in Javascript
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 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)
您需要将对象转换为数组对象并访问它。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.