英文:
Convert ajax response with jQuery
问题
以下是翻译好的部分:
The object
[
{
"name":"Elly",
"phone":"4345353534",
"date":"2023-06-20",
"time":"10:30:00",
"comment":"Some comment here",
"timestamp":"2023-06-06 09:16:28",
"badge": "badge bg-success",
"color": "green"
},
{
"name":"Peter",
"phone":"3943476534875",
"date":"2023-06-20",
"time":"18:00:00",
"comment":"No comment",
"timestamp":"2023-06-06 06:16:28",
"badge": "badge bg-danger",
"color": "red"
}
]
The AJAX Call
$(document).on('click', '#bookings', function() {
var account = $('#account').val();
$.ajax({
url:"bookings.php",
method:"POST",
data:{account:account},
success:function(data) {
// 以下是 AJAX 响应中的 JSON 结果
// 下面的函数放在这里
}
});
});
The function
$(function (e) {
var calendar = $("#calendar").calendarGC({
dayBegin: 0,
prevIcon: '<',
nextIcon: '>',
onPrevMonth: function (e) {
console.log("prev");
console.log(e)
},
onNextMonth: function (e) {
console.log("next");
console.log(e)
},
events: [
{
date: new Date("2023-06-07"),
eventName: "Holiday",
className: "badge bg-danger",
onclick(e, data) {
console.log(data);
},
dateColor: "red"
},
{
date: new Date("2023-06-07"),
eventName: "Holiday with wife",
className: "badge bg-danger",
onclick(e, data) {
console.log(data);
},
dateColor: "red"
}
],
onclickDate: function (e, data) {
console.log(e, data);
}
});
})
The expected result
$(function (e) {
var calendar = $("#calendar").calendarGC({
dayBegin: 0,
prevIcon: '<',
nextIcon: '>',
onPrevMonth: function (e) {
console.log("prev");
console.log(e)
},
onNextMonth: function (e) {
console.log("next");
console.log(e)
},
events: [
{
date: new Date("2023-06-20"),
eventName: "Elly",
className: "badge bg-success",
onclick(e, data) {
console.log(data);
},
dateColor: "green"
},
{
date: new Date("2023-06-20"),
eventName: "Peter",
className: "badge bg-danger",
onclick(e, data) {
console.log(data);
},
dateColor: "red"
}
],
onclickDate: function (e, data) {
console.log(e, data);
}
});
})
What I have tried is
var json = $.parseJSON(data);
var array = [];
json.forEach((item) => {
item = {}
item ["date"] = new Date('"' + item.date + '"');
item ["eventName"] = item.name;
item ["className"] = item.badge;
item ["dateColor"] = item.color;
jsonObj.push(item);
});
请注意,代码块中的特殊字符已被还原为正常的字符。如果您需要进一步的帮助,请告诉我。
英文:
I have an JSON that is coming as a response from ajax call
The object
[
{
"name":"Elly",
"phone":"4345353534",
"date":"2023-06-20",
"time":"10:30:00",
"comment":"Some comment here",
"timestamp":"2023-06-06 09:16:28",
"badge": "badge bg-success"
"color": "green"
},
{
"name":"Peter",
"phone":"3943476534875",
"date":"2023-06-20",
"time":"18:00:00",
"comment":"No comment",
"timestamp":"2023-06-06 06:16:28",
"badge": "badge bg-danger"
"color": "red"
}
]
The AJAX Call
$(document).on('click', '#bookings', function() {
var account = $('#account').val();
$.ajax({
url:"bookings.php",
method:"POST",
data:{account:account},
success:function(data) {
//the JSON result
//The function below is placed here
}
});
});
Then I have this function where I try to create the events
with the data from the AJAX response. In this example, the AJAX return just 2 records, but they might be any number.
The function
$(function (e) {
var calendar = $("#calendar").calendarGC({
dayBegin: 0,
prevIcon: '&#x3c;',
nextIcon: '&#x3e;',
onPrevMonth: function (e) {
console.log("prev");
console.log(e)
},
onNextMonth: function (e) {
console.log("next");
console.log(e)
},
events: [
{
date: new Date("2023-06-07"),
eventName: "Holiday",
className: "badge bg-danger",
onclick(e, data) {
console.log(data);
},
dateColor: "red"
},
{
date: new Date("2023-06-07"),
eventName: "Holiday with wife",
className: "badge bg-danger",
onclick(e, data) {
console.log(data);
},
dateColor: "red"
}
],
onclickDate: function (e, data) {
console.log(e, data);
}
});
})
So, function with the new events
to become like this
The expected result
$(function (e) {
var calendar = $("#calendar").calendarGC({
dayBegin: 0,
prevIcon: '&#x3c;',
nextIcon: '&#x3e;',
onPrevMonth: function (e) {
console.log("prev");
console.log(e)
},
onNextMonth: function (e) {
console.log("next");
console.log(e)
},
events: [
{
date: new Date("2023-06-20"),
eventName: "Elly",
className: "badge bg-success",
onclick(e, data) {
console.log(data);
},
dateColor: "green"
},
{
date: new Date("2023-06-20"),
eventName: "Peter",
className: "badge bg-danger",
onclick(e, data) {
console.log(data);
},
dateColor: "red"
}
],
onclickDate: function (e, data) {
console.log(e, data);
}
});
})
What I have tried is
var json = $.parseJSON(data);
var array = [];
json.forEach((item) => {
item = {}
item ["date"] = new Date('"'+item.date+'"');
item ["eventName"] = item.name;
item ["className"] = item.badge;
item ["dateColor"] = item.color;
jsonObj.push(item);
});
答案1
得分: 1
- 每次都覆盖项目
- 这更简单
const data = `[ { "name":"Elly", "phone":"4345353534", "date":"2023-06-20", "time":"10:30:00", "comment":"Some comment here", "timestamp":"2023-06-06 09:16:28", "badge": "badge bg-success", "color": "green" }, { "name":"Peter", "phone":"3943476534875", "date":"2023-06-20", "time":"18:00:00", "comment":"No comment", "timestamp":"2023-06-06 06:16:28", "badge": "badge bg-danger", "color": "red" } ]`;
const obj = $.parseJSON(data);
const array = obj.map((item) => ({
"date": new Date('"' + item.date + '"'),
"eventName": item.name,
"className": item.badge,
"dateColor": item.color
}));
console.log(array)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
英文:
- You overwrite item each time
- This is simpler
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const data = `[ { "name":"Elly", "phone":"4345353534", "date":"2023-06-20", "time":"10:30:00", "comment":"Some comment here", "timestamp":"2023-06-06 09:16:28", "badge": "badge bg-success", "color": "green" }, { "name":"Peter", "phone":"3943476534875", "date":"2023-06-20", "time":"18:00:00", "comment":"No comment", "timestamp":"2023-06-06 06:16:28", "badge": "badge bg-danger", "color": "red" } ]`;
const obj = $.parseJSON(data);
const array = obj.map((item) => ({
"date": new Date('"' + item.date + '"'),
"eventName": item.name,
"className": item.badge,
"dateColor": item.color
}));
console.log(array)
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论