jquery复制json对象
我正在迭代一组 json 数据,其中包含一些数据库表的结果。在其他数据中,我有一个 RateTable ...erm... 表和一个 Resources 表。 RateTable 有一个属性名称 ResourceId,它链接到资源记录。
因此,我正在迭代我的 RateTable,我需要引用我的资源记录并通过将其复制到它自己的变量中来使用它。这是我到目前为止所遇到的:
if (data)
{
var rs = data.Resources;
$.each(data.RateTables, function(i,item){
if (item.RateTableTypeId == 91)
{
var r = getresource(item.SupplierResourceId, rs)
if (r)
customer_options += '<option value="' + r.ResourceId + '">' + r.Name + '<\/option>';
}
else if (item.RateTableTypeId == 92)
{
var r = getresource(item.CustomerResourceId, rs)
if (r)
supplier_options += '<option value="' + r.ResourceId + '">' + r.Name + '<\/option>';
}
});
$(".ddl-customer").html(customer_options);
$(".ddl-supplier").html(supplier_options);
}
function getresource(id, items)
{
$.each(items, function(i,item){
if (item.ResourceId == id)
return $.extend(true, {}, item);
});
}
我遇到的问题是 getresource 没有返回变量 r 中我的资源项的副本。为什么?
抱歉,我无法发布一些 json 数据,它绝对巨大,这就是为什么我们将引用留给客户端以减少数据负载。我希望有足够的内容可以帮助别人了解我正在尝试做的事情。
劳埃德
I'm iterating through a set of json data which carries results of a few database tables. Amongst other data I have a RateTable ...erm... table, and a Resources table. The RateTable has a property name ResourceId which links to the Resources record.
So, I'm iterating through my RateTable and I need to reference my Resource record and use it by copying it into it's own variable. Here is what I have so far:
if (data)
{
var rs = data.Resources;
$.each(data.RateTables, function(i,item){
if (item.RateTableTypeId == 91)
{
var r = getresource(item.SupplierResourceId, rs)
if (r)
customer_options += '<option value="' + r.ResourceId + '">' + r.Name + '<\/option>';
}
else if (item.RateTableTypeId == 92)
{
var r = getresource(item.CustomerResourceId, rs)
if (r)
supplier_options += '<option value="' + r.ResourceId + '">' + r.Name + '<\/option>';
}
});
$(".ddl-customer").html(customer_options);
$(".ddl-supplier").html(supplier_options);
}
function getresource(id, items)
{
$.each(items, function(i,item){
if (item.ResourceId == id)
return $.extend(true, {}, item);
});
}
The problem I have is that getresource isn't returning a copy of my Resource item in the variable r. Why?
Sorry I can't post some of the json data, it's absolutely huge, which is why we are leaving the referencing to the client side to cut down on the data payload. I'm hoping there is enough to help someone see what I'm trying to do.
Lloyd
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
getresource 没有 return 语句。 getresource 内的 $.each() 调用使用的匿名函数内有一个 return 语句。该 return 语句从匿名函数返回并返回到 $.each()。 $.each() 将返回值(如果有)解释为一个布尔值,指示是否继续迭代。但该值永远不会传递回 getresource,因为 getresource 无论如何都没有 return 语句来返回它。
请参阅http://docs.jquery.com/Utilities/jQuery.each#objectcallback 有关 $.each() 的详细信息。
一种可能的解决方法是使用 javascript 的本机 for 循环,例如,
另一种选择是继续使用 $.each() 但从 getresource 返回,例如
getresource doesn't have a return statement. There is a return statement inside the anonymous function used by the $.each() call inside getresource. That return statement returns from the anonymous function and back into $.each(). $.each() interprets the returned value -- if there is one -- as a boolean indicating whether to continue iterating. But that value never gets passed back to getresource, which doesn't have a return statement to return it anyway.
See http://docs.jquery.com/Utilities/jQuery.each#objectcallback for details about $.each().
One possible fix is to use javascript's native for loop instead, e.g.
Another option is to keep using $.each() but return from getresource, e.g.
我承认我仍在学习,我从未见过有人从函数返回
$.extend
...Oren 是正确的,如果找不到值,您需要返回 false,所以我会给他+1。
此外,我尝试使用
return $.extend(false, {}, items);
测试您的函数,并且if (r)
似乎始终为 true,因为r
是一个对象,我猜您正在尝试从函数返回两个变量?无论如何,从函数返回多个变量的最佳方法是使用如下内容:
然后 r[0] 包含布尔值,r[1] 包含项目(如果为 true)
I admit I'm still learning and I've never seen anyone return an
$.extend
from a function...Oren is right about you needing to return a false if the value is not found, so I'll give him +1.
Additionally, I attempted to test your function using
return $.extend(false, {}, items);
and it appears thatif (r)
will always true becauser
is an object,I'm guessing you are trying to return two variables from a function? Anyway the best way to return mutliple variables from a function would be to use something like this:
then r[0] contains the boolean and r[1] contains the item (if true)