javascript jQuery .getJSON 返回数组变量和 json 数组操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5273650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery .getJSON return into array variable & json array manipulation
提问by ZolaKt
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.
有什么方法可以将 $.getJSON 返回到变量数组中吗?
我知道它是异步的并且超出范围,但我将在 ajax 回调中使用它,我只需要首先获取所有值并根据另一个数组检查它们。
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
编辑: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}]
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
- 获取所有值
- 隔离 JSON 对象的 id 值
- 循环另一个数组
- 检查 json id 是否在另一个数组中
- 通过id值访问其他json变量
There are various solutions here I guess, but I'm looking for something with minimal code.
我猜这里有各种解决方案,但我正在寻找代码最少的东西。
采纳答案by Felix Kling
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 => objectmapping:
根据给定的信息,没有捷径可以测试 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`
}
}
}
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, dataor someOtherArrayand adjust your data structures such that you loop over the smaller array only.
您还应该考虑哪些数组将包含更多元素,data或者someOtherArray调整您的数据结构,以便仅循环遍历较小的数组。
Update:
更新:
To create the appropriate structure on the server with PHP, you have to create an associate array.
要使用 PHP 在服务器上创建适当的结构,您必须创建一个关联数组。
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
回答by rsp
If you get an array as your JSON response then your datavariable in your callback is an array, no need to do anything with it.
如果你得到一个数组作为你的 JSON 响应,那么你data回调中的变量就是一个数组,不需要对它做任何事情。
If you get an object as your JSON response as the data.idin you example might suggest, and some of it's values is an array, then just use data.idas an array, or use var array = data.id;if that is more convenient for you.
如果你得到一个对象作为你的 JSON 响应,就像data.id你的例子中可能建议的那样,并且它的一些值是一个数组,那么就data.id作为一个数组使用,或者var array = data.id;如果这对你更方便的话,就使用它。
Remember that datain 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.
请记住,data在您的回调中只是您作为 JSON 获得的任何内容。它可以是一个对象(它是一个关联数组)、一个数组、一个字符串、一个数字,或者一个真、假或空值。如果它是一个对象,则使用 访问它data.key,如果它是一个数组,则使用 访问它data[index]。我这么说是因为我怀疑你可能会在这里混淆数组和对象。

