javascript - Coldfusion - 使用列表

发布于 2024-12-03 08:11:23 字数 318 浏览 0 评论 0原文

这对某人来说可能很容易。

我正在通过 JSON 将营销活动 ID (12,45,66) 列表返回到 javascript 变量

var campaignList = res.DATA.CAMPAIGNS

现在,给定在 URL 中传递的指定营销活动 ID,

var campaignId ='<cfoutput>#url.campaignID#</cfoutput>'

我想检查返回的列表是否包含此营销活动 ID

非常感谢任何帮助。

This is probably easy for someone.

I am returning a list of campaignIDs (12,45,66) via JSON to a javascript variable

var campaignList = res.DATA.CAMPAIGNS

Now, given a specified campaignID passed in the URL

var campaignId ='<cfoutput>#url.campaignID#</cfoutput>'

I want to check if the returned list contains this campaignID

Any help much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

比忠 2024-12-10 08:11:23

有很多方法可以做到这一点,但我喜欢漂亮的数据结构,所以......

用逗号分割列表,然后循环列表,寻找值:

function campaignExists(campaignList,campaignId) {
    aCampaignList = campaignList.split(',');
    for (i=0;i<aCampaignList.length;i++) {
        if (aCampaignList[i]==campaignId)
            return true;
    }
    return false;
}

Plenty of ways to do it, but I like nice data structures, so ...

Split the list on comma, then loop over list, looking for value:

function campaignExists(campaignList,campaignId) {
    aCampaignList = campaignList.split(',');
    for (i=0;i<aCampaignList.length;i++) {
        if (aCampaignList[i]==campaignId)
            return true;
    }
    return false;
}
一身骄傲 2024-12-10 08:11:23

由于 Array.indexOf 遗憾的是它不是跨浏览器的,因此您会看到类似的内容:

// assume there is no match
var match_found = false;

// iterate over the campaign list looking for a match,
// set "match_found" to true if we find one
for (var i = 0; i < campaignList.length; i += 1) {
    if (parseInt(campaignList[i]) === parseInt(campaignId)) {
        match_found = true;
        break;
    }
}

如果您需要重复执行此操作,请将其包装在函数中

Since Array.indexOf sadly isn't cross browser, you're looking at something like:

// assume there is no match
var match_found = false;

// iterate over the campaign list looking for a match,
// set "match_found" to true if we find one
for (var i = 0; i < campaignList.length; i += 1) {
    if (parseInt(campaignList[i]) === parseInt(campaignId)) {
        match_found = true;
        break;
    }
}

If you need to do this repeatedly, wrap it in a function

青春如此纠结 2024-12-10 08:11:23

这是一个“开箱即用”的解决方案。您可以为您的属性 ID 创建一个结构,并将其传递给 json Searilizer,使其键和值相同。然后您可以测试结构体的 hasOwnProperty。例如:

var campaignIDs = {12 : 12, 45 : 45, 66 : 66};

campaignIDs.hasOwnProperty("12"); //true
campaignIDs.hasOwnProperty("32"); //false

这样,如果列表很长,您就不必循环遍历所有潜在属性来查找匹配项。这是一个查看其实际效果的小提琴:

http://jsfiddle.net/bittersweetryan/NeLfk/

Here's a bit of a "out of the box" solution. You could create a struct for your property id's that you pass into the json searilizer have the key and the value the same. Then you can test the struct for hasOwnProperty. For example:

var campaignIDs = {12 : 12, 45 : 45, 66 : 66};

campaignIDs.hasOwnProperty("12"); //true
campaignIDs.hasOwnProperty("32"); //false

This way if the list is pretty long you wont have to loop through all of the potential properties to find a match. Here's a fiddle to see it in action:

http://jsfiddle.net/bittersweetryan/NeLfk/

逆光下的微笑 2024-12-10 08:11:23

我不喜欢比利对此的回答,函数内的变量已在全局范围内声明,并且有点过于复杂。如果你的 js 中有一个字符串形式的 id 列表,只需从用户输入中搜索你的 id 即可。

<代码>

var patt = new RegExp("(^|,)" + campaignId + "(,|$)");
var foundCampaign = campaignList.search(patt) != -1;

<代码>

I don't like Billy's answer to this, variables within the function have been declared in the global scope and it is somewhat over complicated. If you have a list of ids as a string in your js just search for the id you have from user input.

var patt = new RegExp("(^|,)" + campaignId + "(,|$)");
var foundCampaign = campaignList.search(patt) != -1;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文