忽略Null导致Jquery

时间:2016-09-14 08:43:27

标签: javascript jquery function null local-storage

我使用导航器的本地存储值创建了一个脚本,并且我已经开发了一个向我发送值的Web服务,这里唯一的问题是我不需要它向我发送Null结果,这是功能:

 function SendNeedHelpLinkTrace() {

    var pLinkVirement = sessionStorage.getItem("pClickVirement");
    var pLinkCarteBancaire = sessionStorage.getItem("pLinkCarteBancaire");
    var pLinkRechargePaiementFactureTelecom = sessionStorage.getItem("pLinkRechargePaiementFactureTelecom");
    var pPaiementVignetteImpotTaxe = sessionStorage.getItem("PaiementVignetteImpotTaxe");
    var pLinkPaiementFactureEauElectricite = sessionStorage.getItem("pPaiementFactureEauElectricite");
    var pLinkServiceFatourati = sessionStorage.getItem("pCatchLinkServiceFatourati");
    var pLinkCihExpress = sessionStorage.getItem("pCatchLinkCihExpress");
    var pLinkEdocuments = sessionStorage.getItem("pCatchLinkEdocuments");


    var lChannelId = "01";
    var lServiceId = "900120";
    var lClientId = document.getElementById('<%= HiddenClientId.ClientID%>').value;

    alert(lClientId);
    var lData;
    var lCollect;

    console.log(lClientId);

        lData = pLinkVirement + " | " + pLinkCarteBancaire + " | " + pLinkRechargePaiementFactureTelecom + " | " + pPaiementVignetteImpotTaxe + " | " + pLinkPaiementFactureEauElectricite + " | " + pLinkServiceFatourati + " | " + pLinkCihExpress + " | " + pLinkEdocuments;

        console.log(lData);

        alert(lData);

        lDataCollected = lClientId + ";" + lChannelId + ";" + lServiceId + ";" + lData;

        console.log(lDataCollected);


        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "http://localhost:9097/CatchEvent.asmx/CollectData",
            data: JSON.stringify({ "pData": lDataCollected }),
            dataType: "json",
            async: true,
            success: function (data, textStatus) {
                if (textStatus == "success") {
                    alert('success');
                }
            },
            error: function (exception) {
                alert('Exeption : ' + exception);
            }
        });
        sessionStorage.clear();

    }

结果是这样的:

空|| 300123 || 900452 ||空||空|| 26332

如何才能显示Null结果?

3 个答案:

答案 0 :(得分:1)

鉴于private PerformanceCounter cpuCounter; public Form1() { InitializeComponent(); InitialiseCPUCounter(); timer1.Start(); } private void timer1_Tick(object sender, EventArgs e) { float tmp = cpuCounter.NextValue(); textBox1.Text = String.Format("{0} %", tmp); } private void InitialiseCPUCounter() { cpuCounter = new PerformanceCounter("Processor Information", "% Processor Time", "_Total"); } 是一个字符串,您可以使用split()将其转换为数组,并轻松制作一个filter(),不包括&#39; Null&#39;数组中的值,最后join()再次生成一个字符串:

&#13;
&#13;
lDataCollected
&#13;
&#13;
&#13;

比,最终解决方案:

var lDataCollected = 'Null || 300123 || 900452 || Null || Null || 26332';
var result = lDataCollected
  .split(' || ')
  .filter(function(item) {
    return item !== 'Null';
  })
  .join(' || ');

console.log(result);

答案 1 :(得分:0)

执行 OR 检查

假设lData = pLinkVirement + " | " + pLinkCarteBancaire + " | " + pLinkRechargePaiementFactureTelecom产生 Null || 300123 || 900452

由于您从sessionStorage获取数据,因此数据将存储为String,因此执行JSON.parse并执行以下操作

注意:(如果字符串为空,则在String.toLowerCase()之前执行JSON.parse()

lData = (pLinkVirement || '') + " | " + (pLinkCarteBancaire || '') + " | " + (pLinkRechargePaiementFactureTelecom || '')

假设您从sessionStorage获取数据pLinkVirement为Null,请执行以下操作

pLinkVirement = pLinkVirement.toLowerCase()
pLinkVirement = JSON.parse(pLinkVirement)
pLinkVirement = (pLinkVirement || '')

答案 2 :(得分:0)

正如@ hindmost所评论的那样,你应该创建一个你需要发送和循环它们的键数组。

但我的个人建议,改为发送对象。这样你就会知道什么是空的,什么不是。

示例代码

// map of keys to fetch
var keysToSend = [
    'pClickVirement'
    'pLinkCarteBancaire'
    'pLinkRechargePaiementFactureTelecom'
    'PaiementVignetteImpotTaxe'
    'pPaiementFactureEauElectricite'
    'pCatchLinkServiceFatourati'
    'pCatchLinkCihExpress'
    'pCatchLinkEdocuments'
]

// Loop to concatinate
var data = keysToSend.reduce(function(p,c){
  var _t = sessionStorage.getItem(c);
  return isEmpty(_t) ? p : p + ' | ' + _t;
}, '')

// Check validity
function isEmpty(val){
  return val === undefined || val === null;
}