使用带条件的循环创建 Javascript 对象
Creating Javascript object using loop with conditions
我有一个 sql table 看起来像这样 :
+------+------------+---------+---------------------+-----------+
| SrNo | BidderName | BidAmt | BidDate | BidTicks |
+------+------------+---------+---------------------+-----------+
| 1 | ARBAAZ | 500.00 | Apr 30 2015 10:27AM | 348854000 |
| 2 | SHEKHAR | 600.00 | Apr 30 2015 10:28AM | 348845000 |
| 3 | SHEKHAR | 800.00 | Apr 30 2015 10:28AM | 348838000 |
+------+------------+---------+---------------------+-----------+
我正在以 JSON 格式将其发送到我的客户端。
我可以通过以下方式获取值:
var data;
Table.forEach(function (field) {
var name = field.BidderName;
var amount = field.BidAmt;
data = "";
});
我正在尝试从 Table
为我的浮点图创建一个看起来像这样的对象:
var dataset = [
{
label: "ARBAAZ",
data: [[348854000, 500.00]],//created from [bidticks,bidamt]
color: "#FF0000",
points: { fillColor: "#FF0000", show: true },
lines: { show: true }
},
{
label: "SHEKHAR",
data: [[348845000, 600.00], [48838000, 800.00]],
color: "#0062E3",
points: { fillColor: "#0062E3", show: true },
lines: { show: true }
}
];
有什么想法,我该如何实现?
我不知道 json 对象中的确切数据结构。
但是,我认为你可以这样做
$.each(data, function(index, statistic) {
if (statistic.BidderName == 'Team Red') {
teamRed.push([statistic.BidAmt, statistic.timeStamp]);
} else {
teamBlue.push([statistic.BidAmt, statistic.timeStamp]);
}
});
现在您可以在 数据集 对象中使用 teamRed 和 teamBlue。
我假设您确实有这个 BidderName,它将区分此行属于蓝队还是红队。
假设您的 json 数据在 var 'data' 中,
你可以通过 filter
函数循环,
var seen = {};
data = data.filter(function(entry) {
var previous;
// Have we seen this label before?
if (seen.hasOwnProperty(entry.label)) {
// Yes, grab it and add this data to it
previous = seen[entry.label];
var data = [entry.BidTicks, entry.BidAmt] ;
previous.data.push(data);
// Don't keep this entry, we've merged it into the previous one
return false;
}
// entry.data probably isn't an array; make it one for consistency
entry.data = [];
var data = [entry.BidTicks, entry.BidAmt] ;
entry.data.push(data);
//delete the attributes which are not required in final output
delete entry.BidTicks;
delete entry.BidAmt;
// Remember that we've seen it
seen[entry.label] = entry;
// Keep this one, we'll merge any others that match into it
return true;
});
工作 fiddle 在这里 --> fiddle to filter the array of objects
关于您的额外属性 'color'、'lines' 等,我相信您现在可以将它们带入最终输出。
我有一个 sql table 看起来像这样 :
+------+------------+---------+---------------------+-----------+
| SrNo | BidderName | BidAmt | BidDate | BidTicks |
+------+------------+---------+---------------------+-----------+
| 1 | ARBAAZ | 500.00 | Apr 30 2015 10:27AM | 348854000 |
| 2 | SHEKHAR | 600.00 | Apr 30 2015 10:28AM | 348845000 |
| 3 | SHEKHAR | 800.00 | Apr 30 2015 10:28AM | 348838000 |
+------+------------+---------+---------------------+-----------+
我正在以 JSON 格式将其发送到我的客户端。
我可以通过以下方式获取值:
var data;
Table.forEach(function (field) {
var name = field.BidderName;
var amount = field.BidAmt;
data = "";
});
我正在尝试从 Table
为我的浮点图创建一个看起来像这样的对象:
var dataset = [
{
label: "ARBAAZ",
data: [[348854000, 500.00]],//created from [bidticks,bidamt]
color: "#FF0000",
points: { fillColor: "#FF0000", show: true },
lines: { show: true }
},
{
label: "SHEKHAR",
data: [[348845000, 600.00], [48838000, 800.00]],
color: "#0062E3",
points: { fillColor: "#0062E3", show: true },
lines: { show: true }
}
];
有什么想法,我该如何实现?
我不知道 json 对象中的确切数据结构。 但是,我认为你可以这样做
$.each(data, function(index, statistic) {
if (statistic.BidderName == 'Team Red') {
teamRed.push([statistic.BidAmt, statistic.timeStamp]);
} else {
teamBlue.push([statistic.BidAmt, statistic.timeStamp]);
}
});
现在您可以在 数据集 对象中使用 teamRed 和 teamBlue。
我假设您确实有这个 BidderName,它将区分此行属于蓝队还是红队。
假设您的 json 数据在 var 'data' 中,
你可以通过 filter
函数循环,
var seen = {};
data = data.filter(function(entry) {
var previous;
// Have we seen this label before?
if (seen.hasOwnProperty(entry.label)) {
// Yes, grab it and add this data to it
previous = seen[entry.label];
var data = [entry.BidTicks, entry.BidAmt] ;
previous.data.push(data);
// Don't keep this entry, we've merged it into the previous one
return false;
}
// entry.data probably isn't an array; make it one for consistency
entry.data = [];
var data = [entry.BidTicks, entry.BidAmt] ;
entry.data.push(data);
//delete the attributes which are not required in final output
delete entry.BidTicks;
delete entry.BidAmt;
// Remember that we've seen it
seen[entry.label] = entry;
// Keep this one, we'll merge any others that match into it
return true;
});
工作 fiddle 在这里 --> fiddle to filter the array of objects
关于您的额外属性 'color'、'lines' 等,我相信您现在可以将它们带入最终输出。