说我有一个像这样的对象
```
{
"ListOfStudents": {
"-LTzCztVLA0PW-duF-3e": {
"StudentInfo": {
"csvTestFile": {
"2018-12-18": {
"attendanceDate": "2018-12-18",
"attendanceStatus": "present"
},
"2018-12-19": {
"attendanceDate": "2018-12-19",
"attendanceStatus": "absent"
},
"2018-12-20": {
"attendanceDate": "2018-12-20",
"attendanceStatus": "present"
}
}
}
},
"-LTz15VLA0PW-duF-123": {
"StudentInfo": {
"csvTestFile": {
"2018-12-18": {
"attendanceDate": "2018-12-18",
"attendanceStatus": "present"
},
"2018-12-19": {
"attendanceDate": "2018-12-19",
"attendanceStatus": "absent"
},
"2018-12-20": {
"attendanceDate": "2018-12-20",
"attendanceStatus": "absent"
}
}
}
},
"-LTz15VL515W-duF-163": {
"StudentInfo": {
"csvTestFile": {
"2018-12-18": {
"attendanceDate": "2018-12-18",
"attendanceStatus": "absent"
},
"2018-12-19": {
"attendanceDate": "2018-12-19",
"attendanceStatus": "absent"
},
"2018-12-20": {
"attendanceDate": "2018-12-20",
"attendanceStatus": "absent"
}
}
}
}
}
}
```
这是我的代码
const names = 'Albert,John,Alex';
saveCSVFile(data) {
const { classUID } = data;
const { currentUser } = firebase.auth();
const student = [];
firebase
.database()
.ref(`/users/${currentUser.uid}/ClassAttendance/${classUID}/ListOfStudents`)
.on('value', snapshot1 => {
snapshot1.forEach(child => {
student.push(child.val().StudentInfo);
const keys = Object.keys(child.val().StudentInfo.csvTestFile);
const dates = keys.map(key => child.val().StudentInfo.csvTestFile[key]);
console.log(dates);
});
});
}
我想要的是将'attendanceStatus'合并到一个字符串中,但是对于每个第一个对象,我想在字符串中放入每个名称,对于每个最后一个对象,我都想放入 \ n'和每个出席状态的 (逗号)。
预期的最终字符串输出:
'Albert,现在,不在,现在\ n
约翰,现在,不在,缺席\ n
亚历克斯,缺席,缺席,缺席\ n'
或者可能是这样的输出
“阿尔伯特,现在,缺席,现在\ n约翰,现在,缺席,缺席\ n亚历克斯,缺席,缺席,缺席\ n'
我一直在考虑如何做一个星期,但是没有运气。请帮助
答案 0 :(得分:0)
我不确定这是否是您要的,但我希望这能使您走上正确的轨道。
2018/12/22 20:12:37 [error] 22034#22034: *81 connect() to unix:/home/user/project/shared/tmp/sockets/puma.sock failed (111: Connection refused) while connecting to upstream, client: xx.xx.xx.xx, server: localhost, request: "GET /api/v1/login HTTP/1.1", upstream: "http://unix:/home/user/project/shared/tmp/sockets/puma.sock:/500.html", host: "xx.xx.xx.xx"
答案 1 :(得分:0)
以下是使用map的实现:
let list = {
"-LTzCztVLA0PW-duF-3e": {
"StudentInfo": {
"csvTestFile": {
"2018-12-18": {
"attendanceDate": "2018-12-18",
"attendanceStatus": "present"
},
"2018-12-19": {
"attendanceDate": "2018-12-19",
"attendanceStatus": "absent"
},
"2018-12-20": {
"attendanceDate": "2018-12-20",
"attendanceStatus": "present"
}
}
}
},
"-LTz15VLA0PW-duF-123": {
"StudentInfo": {
"csvTestFile": {
"2018-12-18": {
"attendanceDate": "2018-12-18",
"attendanceStatus": "present"
},
"2018-12-19": {
"attendanceDate": "2018-12-19",
"attendanceStatus": "absent"
},
"2018-12-20": {
"attendanceDate": "2018-12-20",
"attendanceStatus": "absent"
}
}
}
},
"-LTz15VL515W-duF-163": {
"StudentInfo": {
"csvTestFile": {
"2018-12-18": {
"attendanceDate": "2018-12-18",
"attendanceStatus": "absent"
},
"2018-12-19": {
"attendanceDate": "2018-12-19",
"attendanceStatus": "absent"
},
"2018-12-20": {
"attendanceDate": "2018-12-20",
"attendanceStatus": "absent"
}
}
}
}
}
const names = 'Albert,John,Alex';
let resultString = Object.keys(list).map((l, index) => {
let csvTestFiles = list[l].StudentInfo.csvTestFile;
return Object.keys(csvTestFiles).map((i, innerIndex) => {
let str = innerIndex === 0 ? `${names.split(',')[index]},` : '';
return str + csvTestFiles[i].attendanceStatus;
}).join(',');
}).join('\\n').concat('\\n'); // replace with '\n' if you want the new line instead, concat is just appending last '\n' to the string
console.log(resultString);