jQuery将prevObject:添加到数组 [英] Jquery is adding prevObject: to array

查看:217
本文介绍了jQuery将prevObject:添加到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对数组中的所有浮点数求和

I want to sum all floats in an array

当我在Chrome控制台中执行此操作

when I do this in the chrome console

$("tr.project_hours").find("td.number").map(function(i,v)
{return parseFloat($(v).attr("data-time"))
})

我知道

[0, 0, 0, 0, 0, 0, 0]

这就是我想要的

但是,在我的代码中,我有更多的$("tr.project_hours"),我想将它们分别求和. 所以,我

But, in my code I have more $("tr.project_hours") and I want to sum them separately. So, I do

$("tr.project_hours").each(function(){
    row = $(this).find('td.total')
    row.html(sumInputsIn($(this).find("td.number").map(function(i,v){
     return parseFloat($(v).attr("data-time"))})));
    })

('td.total')是应显示结果的列.问题是最后一个代码在控制台中将其返回

('td.total') is the column where the result should be displayed. The problem is that the last code returns this in the console

[0, 0, 0, 0, 0, 0, 0, prevObject: jQuery.fn.jQuery.init[7], context: <tr>]

结果就是臭名昭著的NaN.

So the result is the notorious NaN.

为什么要在数组中获取prevObject?而我该如何重构以摆脱它呢?

Why do I get the prevObject in my array? And how can I refactor to get rid of it?

推荐答案

这应该可以实现预期的结果:

This should carry out the expected result:

$("tr.project_hours").each(function(index, element) {
    var row = $(this).find('td.total');
    var total = 0;
    $(this).find("td.number").each(function (index, element) {
        total += parseFloat($(v).attr("data-time"));
    });
    row.html(total);
});

jQuery映射执行for (item in array),如果您使用hasOwnProperty()进行过滤,则显然不会被hasOwnProperty()过滤正在获得那些钥匙.在这种情况下,您需要自己编写逻辑.

jQuery map performs for (item in array) which apparently does not filter by hasOwnProperty() if you are getting those keys. In this case you need to write the logic yourself I guess.

这篇关于jQuery将prevObject:添加到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆