jQuery .getJSON 返回数组变量 & json数组操作
有什么方法可以将 $.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据给定的信息,没有捷径可以测试 ID 的存在。你真的必须遍历所有内容。但是,您可以通过创建
id => 来改进一点。 object
映射:如果您已经在服务器上创建了这个结构,那就更好了,那么它会是:
您还应该考虑哪些数组将包含更多元素,
data
或someOtherArray
并调整数据结构,以便仅循环较小的数组。更新:
要使用 PHP 在服务器上创建适当的结构,您必须创建一个关联数组。
因此,在将对象添加到数组时,您不应该这样做,
但
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:It woud be even better if you create this structure on the server already, then it would be:
You should also consider which arrays will contain more elements,
data
orsomeOtherArray
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
but
如果您获得一个数组作为 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 usedata.id
as an array, or usevar 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 usingdata.key
, if it is an array you access it usingdata[index]
. I say it because I suspect that you might be confusing arrays with objects here.