用jQuery转换ajax响应

huangapple go评论68阅读模式
英文:

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: '<',
			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);
			}
		});
	})

So, function with the new events to become like this

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);
});

答案1

得分: 1

  1. 每次都覆盖项目
  2. 这更简单
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>
英文:
  1. You overwrite item each time
  2. This is simpler

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const data = `[ { &quot;name&quot;:&quot;Elly&quot;, &quot;phone&quot;:&quot;4345353534&quot;, &quot;date&quot;:&quot;2023-06-20&quot;, &quot;time&quot;:&quot;10:30:00&quot;, &quot;comment&quot;:&quot;Some comment here&quot;, &quot;timestamp&quot;:&quot;2023-06-06 09:16:28&quot;, &quot;badge&quot;: &quot;badge bg-success&quot;, &quot;color&quot;: &quot;green&quot; }, { &quot;name&quot;:&quot;Peter&quot;, &quot;phone&quot;:&quot;3943476534875&quot;, &quot;date&quot;:&quot;2023-06-20&quot;, &quot;time&quot;:&quot;18:00:00&quot;, &quot;comment&quot;:&quot;No comment&quot;, &quot;timestamp&quot;:&quot;2023-06-06 06:16:28&quot;, &quot;badge&quot;: &quot;badge bg-danger&quot;, &quot;color&quot;: &quot;red&quot; } ]`;


const obj = $.parseJSON(data);
const array = obj.map((item) =&gt; ({

  &quot;date&quot;: new Date(&#39;&quot;&#39; + item.date + &#39;&quot;&#39;),
  &quot;eventName&quot;: item.name,
  &quot;className&quot;: item.badge,
  &quot;dateColor&quot;: item.color
}));

console.log(array)

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月6日 16:44:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定