[英]How to merge objects into one string
Say i have an object like this说我有一个这样的对象
```
{
"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"
}
}
}
}
}
}
```
Here's my code这是我的代码
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);
});
});
}
Here's the console log output这是控制台日志输出
What i wanted is to merge 'attendanceStatus' into one string but for every first object i want to put each names in the string, and for every last object i want to put '\\n' on it and a , (comma) for each attendanceStatus.我想要的是将'attendanceStatus'合并到一个字符串中,但是对于每个第一个对象,我想将每个名称放在字符串中,对于每个最后一个对象,我想在其上放置'\\n'和一个, (逗号)出席状态。
Expected Final Output in String:字符串中的预期最终输出:
'Albert,present,absent,present\\n
'阿尔伯特,出席,缺席,出席\\n
John,present,absent,absent\\n
约翰,出席,缺席,缺席\\n
Alex,absent,absent,absent\\n'
亚历克斯,缺席,缺席,缺席\\n'
or may be the output would be like this或者可能是这样的输出
'Albert,present,absent,present\\nJohn,present,absent,absent\\nAlex,absent,absent,absent\\n'
'阿尔伯特,出席,缺席,出席\\n约翰,出席,缺席,缺席\\n亚历克斯,缺席,缺席,缺席\\n'
I have been thinking on how to do this for a week, but no luck.我一直在思考如何做到这一点一周,但没有运气。 Please help
请帮忙
I'm not sure if this is what you are asking for but I hope this gets you on the right track.我不确定这是否是您所要求的,但我希望这能让您走上正轨。
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" } } } } }, result = ""; for(let s in list) { let c = list[s].StudentInfo.csvTestFile; //get the object with the dates for(let d in c) { result += c[d].attendanceStatus + ','; //add the status with a comma } result = result.slice(0, result.length -1); //remove the trailling comma result += '\\n'; //add a newline char for every student } console.log(result); //yay
Here is the implementation using map instead:下面是使用 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.