将新列表添加到现有 java 脚本对象
Adding new list to existing java script object
我正在尝试填充条形图的数据集。我想将 'dataset' 添加到现有的 barData1 对象。
但是我遇到了以下错误。我在这里做错了什么?
error: jquery-3.1.1.min.js:2 Uncaught TypeError: Cannot read property
'splice' of undefined
<script type="text/javascript">
var jsprojStatList = @Html.Raw(ViewBag.projStatList);
$(document).ready(function () {
var barData1 = {
labels: ["No. Of Line Items", "Line Items Ordered", "( % )", "Line Items to be Ordered", "( % )", "PO Generated", "PO Approved", "PO Waiting for Approval"]
};
//var obj = JSON.parse(barData1);
//var parse_obj = JSON.parse(barData1);
var i = 0;
for (var key in jsprojStatList) {
if (jsprojStatList.hasOwnProperty(key)) {
var val = jsprojStatList[key];
barData1['datasets'].splice(i, 0, {
"label": val.BudgetTypeTitle, "backgroundColor": "rgba(220, 220, 220, 0.5)", "rgba(220, 220, 220, 0.5)": "#fff"
, "data": [val.NoOfBudgetItems, val.NoOfBudgetItemsOrdered, val.NoOfBudgetItemsOrderedPercentage, val.NoOfBudgetItemsToBeOrdered
, val.NoOfBudgetItemsToBeOrderedPercentage, val.POGenerated, val.POApproved, (val.POGenerated - val.POApproved)]
});
i++;
};
};
//}
var barOptions1 = {
responsive: true
};
var ctx21 = document.getElementById("barChart1").getContext("2d");
new Chart(ctx21, { type: 'bar', data: barData1, options: barOptions1
});
</script>
您必须先将 barData1['datasets']
本身定义为数组,然后才能向其添加值:
var barData1 = {
datasets: [], // populated below
labels: ["No. Of Line Items", "Line Items Ordered", "( % )", "Line Items to be Ordered", "( % )", "PO Generated", "PO Approved", "PO Waiting for Approval"]
};
.splice()
不会在第一次使用时t/can 自动为您创建数组。
旁注:
由于您似乎没有将 i
用于其他用途,因此可以使用 .push(value)
代替 .splice(i, 0, value)
。那会让你丢弃 var i = 0
和 i++
.
如果jsprojStatList
是一个Array
(根据名字中的List
猜测),a for..in
loop isn't usually appropriate with them.
我正在尝试填充条形图的数据集。我想将 'dataset' 添加到现有的 barData1 对象。
但是我遇到了以下错误。我在这里做错了什么?
error: jquery-3.1.1.min.js:2 Uncaught TypeError: Cannot read property 'splice' of undefined
<script type="text/javascript">
var jsprojStatList = @Html.Raw(ViewBag.projStatList);
$(document).ready(function () {
var barData1 = {
labels: ["No. Of Line Items", "Line Items Ordered", "( % )", "Line Items to be Ordered", "( % )", "PO Generated", "PO Approved", "PO Waiting for Approval"]
};
//var obj = JSON.parse(barData1);
//var parse_obj = JSON.parse(barData1);
var i = 0;
for (var key in jsprojStatList) {
if (jsprojStatList.hasOwnProperty(key)) {
var val = jsprojStatList[key];
barData1['datasets'].splice(i, 0, {
"label": val.BudgetTypeTitle, "backgroundColor": "rgba(220, 220, 220, 0.5)", "rgba(220, 220, 220, 0.5)": "#fff"
, "data": [val.NoOfBudgetItems, val.NoOfBudgetItemsOrdered, val.NoOfBudgetItemsOrderedPercentage, val.NoOfBudgetItemsToBeOrdered
, val.NoOfBudgetItemsToBeOrderedPercentage, val.POGenerated, val.POApproved, (val.POGenerated - val.POApproved)]
});
i++;
};
};
//}
var barOptions1 = {
responsive: true
};
var ctx21 = document.getElementById("barChart1").getContext("2d");
new Chart(ctx21, { type: 'bar', data: barData1, options: barOptions1
});
</script>
您必须先将 barData1['datasets']
本身定义为数组,然后才能向其添加值:
var barData1 = {
datasets: [], // populated below
labels: ["No. Of Line Items", "Line Items Ordered", "( % )", "Line Items to be Ordered", "( % )", "PO Generated", "PO Approved", "PO Waiting for Approval"]
};
.splice()
不会在第一次使用时t/can 自动为您创建数组。
旁注:
由于您似乎没有将
i
用于其他用途,因此可以使用.push(value)
代替.splice(i, 0, value)
。那会让你丢弃var i = 0
和i++
.如果
jsprojStatList
是一个Array
(根据名字中的List
猜测),afor..in
loop isn't usually appropriate with them.