小编nra*_*rez的帖子

更好地计算数组中属性值的方法

我有这样的事情:

$scope.traveler = [
            {  description: 'Senior', Amount: 50},
            {  description: 'Senior', Amount: 50},
            {  description: 'Adult', Amount: 75},
            {  description: 'Child', Amount: 35},
            {  description: 'Infant', Amount: 25 },
];
Run Code Online (Sandbox Code Playgroud)

现在有了这个数组的总量,我正在做这样的事情:

$scope.totalAmount = function(){
       var total = 0;
       for (var i = 0; i < $scope.traveler.length; i++) {
              total = total + $scope.traveler[i].Amount;
            }
       return total;
}
Run Code Online (Sandbox Code Playgroud)

当只有一个数组时很容易,但我有其他数组,我想要总结一个不同的属性名称.

我会更高兴如果我可以做这样的事情:

$scope.traveler.Sum({ Amount });
Run Code Online (Sandbox Code Playgroud)

但我不知道如何通过这种方式来解决这个问题,我可以在将来重复使用它,如下所示:

$scope.someArray.Sum({ someProperty });
Run Code Online (Sandbox Code Playgroud)

回答

我决定使用@gruff-bunny建议,所以我避免原型对象(Array)的原型设计

我只是对他的答案做了一点修改,验证了数组,并且sum的值不为null,这是我的最终实现:

$scope.sum = function (items, prop) {
    if …
Run Code Online (Sandbox Code Playgroud)

javascript arrays prototype-programming

136
推荐指数
13
解决办法
15万
查看次数

使用来自js的Phonegap 3.0调用活动方法的最佳方法

我正在尝试使用MainActivity中的本机方法通过phonegap中的index.html拨打电话.

我正在使用phonegap 3.0和android 4.3平台.我在这篇文章中尝试了第二个答案,但它不适用于这个版本.

我想知道解决这个问题的最佳方法是什么?

android phone-call cordova

6
推荐指数
1
解决办法
5717
查看次数

自定义AuthorizeAttributte与Enum Roles params在ajax调用中获取null值

我的自定义AuthorizeAttribute遇到了一些问题

public class ExplicitAuthorizeAttribute : AuthorizeAttribute
{
    private readonly MembershipUserRole[] _acceptedRoles;

    public ExplicitAuthorizeAttribute()
    {

    }

    public ExplicitAuthorizeAttribute(params MembershipUserRole[] acceptedRoles)
    {
        _acceptedRoles = acceptedRoles;
    }

    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {  
       //Validation ...          
    }
}
Run Code Online (Sandbox Code Playgroud)

我这样使用它:

[ExplicitAuthorize[(MembershipUserRole.Admin, MembershipUserRole.SuperAdmin)]
Run Code Online (Sandbox Code Playgroud)

它非常适合HttpGet和HttpPost验证我的控制器和方法.

但是当我在ApiController中使用它并进行ajax调用时,AuthorizeCore没有运行,我遇到了安全漏洞.:/

我的枚举看起来像这样

[Flags]
public enum MembershipUserRole
{
    Admin= 1,
    SuperAdmin = 2
}
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么我的AuthorizeCore在这种情况下没有验证?

顺便说一下如果我用

[Authorized(Roles ="Admin, SuperAdmin")]
Run Code Online (Sandbox Code Playgroud)

这是完美的验证,但我想要Stronly Typed Roles,这就是我使用枚举的原因.

c# api ajax authorize-attribute asp.net-mvc-4

5
推荐指数
1
解决办法
1778
查看次数

使用 int、double 和 long 计算幂

我无法弄清楚何时使用 int、double 和 long。

我正在计算整数的幂并返回结果,只要提供的幂不是负数。

对于作业,我需要使用以下代码开始:

public static long powerN(int number, int power) {
Run Code Online (Sandbox Code Playgroud)

这是我想出的:

public class PowerCalculator
{
/**
 * Calculate the non-negative power of an integer number. If a negative power is input, the method returns 1.
 * 
 * @param number The number to take power.
 * @param power The power factor to be taken to.
 * @return The calculation result after taking power of the integer number.
 */
    public static long powerN(int number, int power) {

       if …
Run Code Online (Sandbox Code Playgroud)

java double int long-integer

5
推荐指数
2
解决办法
2万
查看次数