getJson grep 中的变量范围
我尝试在 getJson 请求的 grep 中使用变量。我希望根据用户的选择(他们单击的链接)来定义变量 - 但我不确定如何将其传递给 grep,因为似乎必须在 getJson 调用中定义该变量?
第一个示例有效:
$.getJSON('data/all.json', function(data)
{
location = $.grep(data, function(el, i)
{
var search=el.clicked_field;
return search === "Y"
});
});
第二个示例无效:
var search=el.clicked_field;
$.getJSON('data/all.json', function(data)
{
location = $.grep(data, function(el, i)
{
return search === "Y"
});
});
我认为这个问题已在 jQuery 论坛中讨论过: http://forum.jquery.com/topic/variable-scoping -and-the-getjson-method
但我无法复制建议的解决方案。任何帮助都会很棒...
Im trying to use a variable within a grep of a getJson request. I want the variable to be defined based on a users' selection (the link they click on) -- but Im not sure how to pass it to the grep because it seems like the variable has to be defined within the getJson call?
This first example works:
$.getJSON('data/all.json', function(data)
{
location = $.grep(data, function(el, i)
{
var search=el.clicked_field;
return search === "Y"
});
});
This 2nd one doesnt:
var search=el.clicked_field;
$.getJSON('data/all.json', function(data)
{
location = $.grep(data, function(el, i)
{
return search === "Y"
});
});
I think the issue was discussed here in the jQuery forum:
http://forum.jquery.com/topic/variable-scoping-and-the-getjson-method
But I havent been able to replicate the proposed solution. Any help would be fantastic...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在第二个 grep 回调中定义了 el ,这就是为什么它在该范围之外未定义的原因。
you're defining
el
in the grep's callback in the 2nd one, which is why it's undefined outside that scope.这两段代码中唯一明显的区别是
var search=el.clicked_field
从$.grep
上下文移到整个代码体之前。此时,el
还不存在。因此,除非您有其他代码片段可以覆盖您的帖子中缺少的内容,否则这就是您的问题。编辑:
JavaScript 小技巧。变量在其范围内是全局的,因此以以下示例为例:
编辑:(再次)
好的,您在逻辑和语法中存在一些基本问题。在注释中一次处理这一方面变得有点乏味,所以我只想说明:
data
是一个 JavaScript 对象列表。将其传递给$.grep
将导致它迭代这些对象。每次迭代,当前对象将存储在el
中,并且该对象在data
数组中的索引将存储在i
中。以这段 JSON 为例:如果这是返回的 JSON,那么
el
将同时具有foo
和bar
属性,您可以使用然后可以用来访问每个值,例如:也许如果您实际上发布了 JSON 响应,我也许可以为您提供进一步的帮助。 (更新您的问题,不要将其作为评论发布)
The only obvious difference in the two pieces of code is the movement of
var search=el.clicked_field
out of the context of$.grep
to before the whole code body. At that point,el
doesn't exist, yet. So, unless you have some other piece of code that covers that missing from your post, that's your problem.EDIT:
Little JavaScript tip. Variables are global within their scope, so take for example the following:
EDIT: (again)
Okay, so you have some fundamental problems in your logic and syntax. It's becoming a bit tedious handling this one aspect at a time in comments, so I'm just going to illustrate:
data
is a list of JavaScript objects. Passing it to$.grep
will cause it to iterate through those objects. Each iteration, the current object will be stored inel
and the index of that object within thedata
array will be stored ini
. Take this bit of JSON as an example:If that was the returned JSON, then
el
would have attributes of bothfoo
andbar
, that you could then use to access the values of each, e.g.:Maybe if you actually post your JSON response, I might be able to assist you further. (Update your question, don't post it as a comment)