gpt4 book ai didi

jQuery/JSON 排序结果

转载 作者:行者123 更新时间:2023-12-01 08:17:35 26 4
gpt4 key购买 nike

我有一个 ColdFusion 方法 getData(),它返回一个查询对象,如下所示:

CustomerCode  ServiceCode  SubscriberCode  Status    UserName
-------------------------------------------------------------
811101 8 gertjan OPEN gertjan@blah.net
811101 8 gertjan CLOSING gertjan@blah.net
811101 2 99652444 CLOSED gertjan@blah.net
811101 2 99655000 OPEN gertjan@blah.net

注意前两行 - 完全相同,除了 Status OPEN 和 CLOSING 分别。

以下函数为 ServiceCode=8 且 Status 为 OPEN 或 CLOSING 的每一行创建一个新的选择选项,前两行都是这种情况。

数据最终来自网络服务,这是我无法控制更改的。我需要更改 jQuery,以便如果同一 ServiceCode/SubscriberCode 组契约(Contract)时存在 OPEN 和 CLOSING 记录(前两行就是这种情况),则仅为 OPEN 记录创建一个选项。

function getInternetLines(){
var CustomerCode=global_customerCode;
var SessionID=global_sessionID;
var lines=[];
$.getJSON("/system.cfc?method=getData&returnformat=json&queryformat=column",
{"SessionID":SessionID,"CustomerCode":CustomerCode},
function(res,code) {
if(res.ROWCOUNT > 0){
for(var i=0; i<res.ROWCOUNT; i++) {
var ServiceCode = res.DATA.ServiceCode[i];
var SubscriberCode = res.DATA.SubscriberCode[i];
var Status = res.DATA.Status[i];
if(ServiceCode == 8 && (Status == 'OPEN' || Status == 'CLOSING')){
lines.push(SubscriberCode);
$('#selInternet').append(
$('<option></option>').val(SubscriberCode).html(SubscriberCode)
);
}
}
global_internet_lines = lines;
if(lines.length == 0){
$('#divBroadbandUsage').html('No Active Broadband Connections.');
}
}else{
$('#divBroadbandUsage').html('No Active Broadband Connections.');
}
});
}

HTML

<select name="selInternet" id="selInternet" style="width:120px">
</select>

非常感谢您提供最简洁的方法来实现这一点,例如,无需对同一数据集进行多次循环。

最佳答案

在读取数据时,您需要保留哈希值,如果已经找到“OPEN”,则忽略数据。然后循环遍历哈希项并输出数据:

if(res.ROWCOUNT > 0){
var hash = {}; // Hash to store data
for(var i=0; i<res.ROWCOUNT; i++) {
var ServiceCode = res.DATA.ServiceCode[i];
var SubscriberCode = res.DATA.SubscriberCode[i];
var Status = res.DATA.Status[i];
if(ServiceCode == 8 && (Status == 'OPEN' || Status == 'CLOSING')){
if( hash[SubscriberCode] != undefined && hash[SubscriberCode].status == 'OPEN' ) {
// If we already have OPEN, don't load the data
continue;
} else {
// Else override whatever data you have for this SubscriberCode
hash[SubscriberCode] = { status: Status, subscriber: SubscriberCode, service: ServiceCode };
}
}
}

// loop through the hash and output the options
for(var x in hash) {
lines.push(hash[x].subscriber);
$('#selInternet').append(
$('<option></option>').val(hash[x].subscriber).html(hash[x].subscriber)
);
}

global_internet_lines = lines;
if(lines.length == 0){
$('#divBroadbandUsage').html('No Active Broadband Connections.');
}
}

我不确定您的情况是什么,但我相信这涵盖了您的描述。我意识到将哈希 key 存储在数据中是愚蠢的,但为了演示,这就是检索其他数据的方式。此代码存储 StatusSubscriberCodeServiceCode,但您的示例仅使用 SubscribercCode。如果确实如此,那就简单多了:

if(res.ROWCOUNT > 0){
var hash = {}; // Hash to store data
for(var i=0; i<res.ROWCOUNT; i++) {
var ServiceCode = res.DATA.ServiceCode[i];
var SubscriberCode = res.DATA.SubscriberCode[i];
var Status = res.DATA.Status[i];
if(ServiceCode == 8 && (Status == 'OPEN' || Status == 'CLOSING')){
// If we see the subscriber code, add it to our hash
hash[SubscriberCode] = 1;
}
}

// loop through the hash and output the options
for(var sub in hash) {
lines.push(sub);
$('#selInternet').append(
$('<option></option>').val(sub).html(sub)
);
}

global_internet_lines = lines;
if(lines.length == 0){
$('#divBroadbandUsage').html('No Active Broadband Connections.');
}
}

关于jQuery/JSON 排序结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9491822/

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