我需要一些帮助,以便在nodejs的JSON数组的末尾添加一个新元素
示例JSON数组
[{
"subject": "physics",
"student_id": "2569",
"values": "0.152,0.228,0.218"
}, {
"subject": "maths",
"student_id": "1236",
"values": "0.146,0.22,0.212"
}, {
"subject": "chemistry",
"student_id": "4569",
"values": "0.159,0.234,0.224"
}, {
"subject": "physics",
"student_id": "1478",
"values": "0.16,0.235,0.225"
}]
预期结果应该是
[{
"subject": "physics",
"student_id": "2569",
"values": "0.152,0.228,0.218"
}, {
"subject": "maths",
"student_id": "1236",
"values": "0.146,0.22,0.212"
}, {
"subject": "chemistry",
"student_id": "4569",
"values": "0.159,0.234,0.224"
}, {
"subject": "physics",
"student_id": "1478",
"values": "0.16,0.235,0.225"
},
lastSyncTime: 1550467657366]
请为我提供一个简单的解决方案。
答案 0 :(得分:0)
您只需使用javaScript
var myArray = [{
"subject": "physics",
"student_id": "2569",
"values": "0.152,0.228,0.218"
}, {
"subject": "maths",
"student_id": "1236",
"values": "0.146,0.22,0.212"
}, {
"subject": "chemistry",
"student_id": "4569",
"values": "0.159,0.234,0.224"
}, {
"subject": "physics",
"student_id": "1478",
"values": "0.16,0.235,0.225"
}];
myArray.push({lastSyncTime: 1550467657366});
console.log(myArray);
结果将是
[{
"subject": "physics",
"student_id": "2569",
"values": "0.152,0.228,0.218"
}, {
"subject": "maths",
"student_id": "1236",
"values": "0.146,0.22,0.212"
}, {
"subject": "chemistry",
"student_id": "4569",
"values": "0.159,0.234,0.224"
}, {
"subject": "physics",
"student_id": "1478",
"values": "0.16,0.235,0.225"
},
{lastSyncTime: 1550467657366}];
您也可以使用
获取最后一项var lastSyncTime = myArray[myArray.length - 1].lastSyncTime;
console.log(lastSyncTime); // That is: 1550467657366
或者,如果您想使用lodash
在列表中查找对象,请查看下面的链接