gpt4 book ai didi

javascript - 如何添加到 AJAX 数组结果

转载 作者:行者123 更新时间:2023-12-03 06:13:05 25 4
gpt4 key购买 nike

我有一个 AJAX 请求,我想向返回的 AJAX 结果添加更多属性。任何帮助都会很棒

这是我的 AJAX 请求代码:

var form_data = {};

$.ajax({
type: "GET",
url: "../../../sample_data/chart1.json",
data: form_data,

success: function( response) {
var result = $(data);
console.log(result.datasets.push(style));
var ctx = document.getElementById("chart_div_won").getContext("2d");
var options = {
responsive: true,
maintainAspectRatio: true,
pointDotRadius: 5,
showXLabels: 5,
};

var myLineChart = new Chart(ctx).LineAlt(response, options);


},
error: function() {
$('div#chart-container').html('<div class="notification-body"><p class="notification-heading">Loading error...</p><p class="notification-description">Unfortunatley for some reason visual data failed to load.</p></div>');
},
dataType: "json",
contentType: "application/json; charset=utf-8",
});

返回以下数据:

{  
"labels":[
"1 Feb",
"8 Feb",
"15 Feb",
"22 Feb",
"29 Feb",
"7 Mar",
"14 Mar",
"21 Mar",
"28 Mar",
"4 Apr",
"11 Apr",
"18 Apr",
"25 Apr"
],
"datasets":[
{

"data":[
77,
55,
40,
65,
59,
80,
81,
56,
55,
65,
59,
80,
75
]
}
]
}

我的问题是我想将以下内容添加/推送到返回的“数据集”数组中(在“数据:”级别):

fillColor:"rgba(253,0,20,0.2)",
strokeColor:"rgba(253,0,20,1)",
pointColor:"#fff",
pointStrokeColor:"rgba(253,0,20,1)",
pointHighlightFill:"#fff",
pointHighlightStroke:"rgba(253,0,20,1)"

这可能吗?如何实现?

最佳答案

您有嵌套数组。所以你需要做这样的事情:

for(var i = 0; i < result.datasets.length; i++)
{
for(var j = 0; j < result.datasets[i].data.length; j++)
{
result.datasets[i].data[j]["fillColor"] = "rgba(253,0,20,0.2)";
result.datasets[i].data[j]["strokeColor"] = "rgba(253,0,20,1)";
result.datasets[i].data[j]["pointColor"] = "#fff";
result.datasets[i].data[j]["pointStrokeColor"] = "rgba(253,0,20,1)";
result.datasets[i].data[j]["pointHighlightFill"] = "#fff";
result.datasets[i].data[j]["pointHighlightStroke"] = "rgba(253,0,20,1)";
}
}

关于javascript - 如何添加到 AJAX 数组结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39234095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com