jQuery .getJSON 返回数组变量 & json数组操作

发布于 2024-10-21 10:38:23 字数 1348 浏览 5 评论 0原文


有什么方法可以将 $.getJSON 的返回值放入变量数组中吗?
我知道它是异步的并且超出了范围,但我将在 ajax 回调中使用它,我只需要先获取所有值并根据另一个数组检查它们。

类似于:

$.getJSON('itemManager.php?a=getItems', function(data){
    // itemArray = new Array(data);
    // idsArray = new Array(data.id);
    for (var i in someOtherArray){
        if($.inArray(i, idsArray) == -1){
            // do something...
            // get jason variable by id?
            // itemArray[i].someVariable
        }
    }
}

编辑: JSON 结构

[{"id":"786","user_id":"1","seller_id":"2","address_id":"1","time":1299852115,"publicComment":null,"personalComment":null},
{"id":"787","user_id":"1","seller_id":"2","address_id":"1","time":1299852115,"publicComment":null,"personalComment":null},
{"id":"785","user_id":"1","seller_id":"2","address_id":"1","time":1299852114,"publicComment":null,"personalComment":null},
{"id":"784","user_id":"1","seller_id":"2","address_id":"1","time":1299852113,"publicComment":null,"personalComment":null},
{"id":"783","user_id":"1","seller_id":"2","address_id":"1","time":1299852111,"publicComment":null,"personalComment":null}]

这基本上就是这个想法。

  • 获取所有值
  • 隔离 JSON 对象的 id 值
  • 循环另一个数组
  • 检查 json id 是否在另一个数组内
  • 通过 id 值访问其他 json 变量

我猜这里有各种解决方案,但我正在寻找代码最少的解决方案。

is there any way I can get the return of $.getJSON into a variable array?
I know its async and out of scope, but I will use it inside ajax callback, I just need to get all the values first and check them against another array.

Something like:

$.getJSON('itemManager.php?a=getItems', function(data){
    // itemArray = new Array(data);
    // idsArray = new Array(data.id);
    for (var i in someOtherArray){
        if($.inArray(i, idsArray) == -1){
            // do something...
            // get jason variable by id?
            // itemArray[i].someVariable
        }
    }
}

EDIT: JSON structure

[{"id":"786","user_id":"1","seller_id":"2","address_id":"1","time":1299852115,"publicComment":null,"personalComment":null},
{"id":"787","user_id":"1","seller_id":"2","address_id":"1","time":1299852115,"publicComment":null,"personalComment":null},
{"id":"785","user_id":"1","seller_id":"2","address_id":"1","time":1299852114,"publicComment":null,"personalComment":null},
{"id":"784","user_id":"1","seller_id":"2","address_id":"1","time":1299852113,"publicComment":null,"personalComment":null},
{"id":"783","user_id":"1","seller_id":"2","address_id":"1","time":1299852111,"publicComment":null,"personalComment":null}]

This is basically the idea.

  • Get all the values
  • Isolate the id values of JSON objects
  • Loop another array
  • Check if json id is inside the other array
  • Access other json variables by id value

There are various solutions here I guess, but I'm looking for something with minimal code.

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

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

发布评论

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

评论(2

秉烛思 2024-10-28 10:38:23

根据给定的信息,没有捷径可以测试 ID 的存在。你真的必须遍历所有内容。但是,您可以通过创建 id => 来改进一点。 object 映射:

$.getJSON('itemManager.php?a=getItems', function(data){
    var items = {};
    for(var i = data.length; i--; ) {
        items[data[i].id] = data[i];
    }
    for (var j = someOtherArray.length; j--; ){
        var item = items[someOtherArray[j]];
        if(item){
            // do something with `item`
        }
    }
}

如果您已经在服务器上创建了这个结构,那就更好了,那么它会是:

$.getJSON('itemManager.php?a=getItems', function(data){
    for (var j = someOtherArray.length; j--; ){
        var item = data[someOtherArray[j]];
        if(item){
            // do something with `item`
        }
    }
}

您还应该考虑哪些数组将包含更多元素,datasomeOtherArray 并调整数据结构,以便仅循环较小的数组。

更新:

要使用 PHP 在服务器上创建适当的结构,您必须创建一个关联数组。

因此,在将对象添加到数组时,您不应该这样做,

$items[] = $obj;

$items[$obj->id] = $obj; // or $obj['id'] if you have an array

With the given information, there is not shortcut to test the existence of IDs. You really have to loop over everything. However you can improve a bit by creating an id => object mapping:

$.getJSON('itemManager.php?a=getItems', function(data){
    var items = {};
    for(var i = data.length; i--; ) {
        items[data[i].id] = data[i];
    }
    for (var j = someOtherArray.length; j--; ){
        var item = items[someOtherArray[j]];
        if(item){
            // do something with `item`
        }
    }
}

It woud be even better if you create this structure on the server already, then it would be:

$.getJSON('itemManager.php?a=getItems', function(data){
    for (var j = someOtherArray.length; j--; ){
        var item = data[someOtherArray[j]];
        if(item){
            // do something with `item`
        }
    }
}

You should also consider which arrays will contain more elements, data or someOtherArray and adjust your data structures such that you loop over the smaller array only.

Update:

To create the appropriate structure on the server with PHP, you have to create an associate array.

So at the point where you add an object to the array, you should not do

$items[] = $obj;

but

$items[$obj->id] = $obj; // or $obj['id'] if you have an array
你的心境我的脸 2024-10-28 10:38:23

如果您获得一个数组作为 JSON 响应,那么回调中的 data 变量就是一个数组,无需对其执行任何操作。

如果您获得一个对象作为 JSON 响应(如示例中的 data.id 所示),并且其中一些值是数组,则只需使用 data.id 作为一个数组,或者使用 var array = data.id;(如果这对您来说更方便)。

请记住,回调中的 data 就是您以 JSON 形式获得的任何内容。它可以是一个对象(关联数组)、数组、字符串、数字或者 true、false 或 null 值。如果它是一个对象,您可以使用 data.key 访问它,如果它是一个数组,您可以使用 data[index] 访问它。我这么说是因为我怀疑您可能在这里将数组与对象混淆了。

If you get an array as your JSON response then your data variable in your callback is an array, no need to do anything with it.

If you get an object as your JSON response as the data.id in you example might suggest, and some of it's values is an array, then just use data.id as an array, or use var array = data.id; if that is more convenient for you.

Remember that data in your callback is just whatever you got as JSON. It can be an object (which is an associative array), an array, a string, a number, or a true, false or null value. If it is an object you access it using data.key, if it is an array you access it using data[index]. I say it because I suspect that you might be confusing arrays with objects here.

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