javascript - Coldfusion - 使用列表
这对某人来说可能很容易。
我正在通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有很多方法可以做到这一点,但我喜欢漂亮的数据结构,所以......
用逗号分割列表,然后循环列表,寻找值:
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:
由于
Array.indexOf
遗憾的是它不是跨浏览器的,因此您会看到类似的内容:如果您需要重复执行此操作,请将其包装在函数中
Since
Array.indexOf
sadly isn't cross browser, you're looking at something like:If you need to do this repeatedly, wrap it in a function
这是一个“开箱即用”的解决方案。您可以为您的属性 ID 创建一个结构,并将其传递给 json Searilizer,使其键和值相同。然后您可以测试结构体的 hasOwnProperty。例如:
这样,如果列表很长,您就不必循环遍历所有潜在属性来查找匹配项。这是一个查看其实际效果的小提琴:
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:
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/
我不喜欢比利对此的回答,函数内的变量已在全局范围内声明,并且有点过于复杂。如果你的 js 中有一个字符串形式的 id 列表,只需从用户输入中搜索你的 id 即可。
<代码>
<代码>
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.