getJson grep 中的变量范围

发布于 2024-11-16 20:32:55 字数 783 浏览 2 评论 0原文

我尝试在 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 技术交流群。

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

发布评论

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

评论(2

云之铃。 2024-11-23 20:32:55

您在第二个 grep 回调中定义了 el ,这就是为什么它在该范围之外未定义的原因。

you're defining el in the grep's callback in the 2nd one, which is why it's undefined outside that scope.

情场扛把子 2024-11-23 20:32:55

这两段代码中唯一明显的区别是 var search=el.clicked_field$.grep 上下文移到整个代码体之前。此时,el 还不存在。因此,除非您有其他代码片段可以覆盖您的帖子中缺少的内容,否则这就是您的问题。

编辑:

JavaScript 小技巧。变量在其范围内是全局的,因此以以下示例为例:

$('#some-element').click(function() {
    var someElement = $(this);

    $.getJSON('url', function(data, jqXHR){
        console.log(someElement); // exists!
    });
});

编辑:(再次)

好的,您在逻辑和语法中存在一些基本问题。在注释中一次处理这一方面变得有点乏味,所以我只想说明:

$('#some-element').click(function() {
    var someElement = $(this).attr('href'); 

    // someElement == 'http://google.com'

    $.getJSON('data/all.json', function(data) {
       location = $.grep(data, function(el, i) {
           clicked = someElement.toLowerCase().slice(1);

           // clicked == 'ttp://google.com'   # Why?

           link = 'el' + '.' + clicked;

           // link == 'el.ttp://google.com'   # Why?

           console.log(link);  // This bit was for illustrative purposes, you should remove it from your code now.
           return link === "Y";

           // "el.ttp://google.com" != "Y"   # Obviously, and never will
       });
    });

data 是一个 JavaScript 对象列表。将其传递给 $.grep 将导致它迭代这些对象。每次迭代,当前对象将存储在el中,并且该对象在data数组中的索引将存储在i中。以这段 JSON 为例:

[ { "foo": 1, "bar": 2 } ]

如果这是返回的 JSON,那么 el 将同时具有 foobar 属性,您可以使用然后可以用来访问每个值,例如:

el.foo == 1 // True
el.bar == 2 // True

也许如果您实际上发布了 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:

$('#some-element').click(function() {
    var someElement = $(this);

    $.getJSON('url', function(data, jqXHR){
        console.log(someElement); // exists!
    });
});

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:

$('#some-element').click(function() {
    var someElement = $(this).attr('href'); 

    // someElement == 'http://google.com'

    $.getJSON('data/all.json', function(data) {
       location = $.grep(data, function(el, i) {
           clicked = someElement.toLowerCase().slice(1);

           // clicked == 'ttp://google.com'   # Why?

           link = 'el' + '.' + clicked;

           // link == 'el.ttp://google.com'   # Why?

           console.log(link);  // This bit was for illustrative purposes, you should remove it from your code now.
           return link === "Y";

           // "el.ttp://google.com" != "Y"   # Obviously, and never will
       });
    });

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 in el and the index of that object within the data array will be stored in i. Take this bit of JSON as an example:

[ { "foo": 1, "bar": 2 } ]

If that was the returned JSON, then el would have attributes of both foo and bar, that you could then use to access the values of each, e.g.:

el.foo == 1 // True
el.bar == 2 // True

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)

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