0

I have the following array.

       "APP-A": {
            "active": true
        },
        "APP-B": {
            "active": true
        },
        "APP-C": {
            "active": true
        },
        "BOOL-A": {
            "active": true
        },
        "BOOL-B": {
            "active": true
        },
        "BOOL-C": {
            "active": true
        },

I need to get the total count of the APP-*'s in the array. Am trying to implement in angular JS. Solutions in plain JS will also be helpful.

TIA

4
  • 5
    That doesn't look like an array. Commented Dec 10, 2014 at 4:58
  • 1
    var count = 0; for (var key in obj) count += /^APP-/.test(key)? Commented Dec 10, 2014 at 5:00
  • It doesn't even look like javascript. Commented Dec 10, 2014 at 5:07
  • Its a JSON response, needed to fetch count in javascript. Commented Dec 10, 2014 at 5:27

6 Answers 6

2

One approach is to use .filter() on the object's keys:

var count = Object.keys(obj).filter(function (key) {
    return key.indexOf(prefix) === 0;
}).length;

http://jsfiddle.net/4m692usx/1/

Sign up to request clarification or add additional context in comments.

Comments

1

may be try like this:

var items = { "APP-A": {
        "active": true
    },
    "APP-B": {
        "active": true
    },
    "APP-C": {
        "active": true
    },
    "BOOL-A": {
        "active": true
    },
    "BOOL-B": {
        "active": true
    },
    "BOOL-C": {
        "active": true
    }
};
var c=0;
for(var item in items)
{
if(item.indexOf("APP-") >= 0)      
c++;
}
console.log(c);// your count here.

Comments

1

var myArray = {
  "APP-A": {
    "active": true
  },
  "APP-B": {
    "active": true
  },
  "APP-C": {
    "active": true
  },
  "BOOL-A": {
    "active": true
  },
  "BOOL-B": {
    "active": true
  },
  "BOOL-C": {
    "active": true
  }
};

var reg = /^APP-/,
    count = 0, 
    name;

for (name in myArray) {
  count += reg.test(name);
}

document.getElementById('output').innerHTML = count;
<div id="output"></div>

Comments

1

That looks like a json, not an array.

var count = 0;
for (var key in json) {
    key.indexOf('APP-')!==-1?count++,count;
}

Comments

1

Check this JSFiddle Link

But actually it's not an array, it's a JSON object. And what we are counting is JSON key's.

JS Part:

var items = { "APP-A": {
            "active": true
        },
        "APP-B": {
            "active": true
        },
        "APP-C": {
            "active": true
        },
        "BOOL-A": {
            "active": true
        },
        "BOOL-B": {
            "active": true
        },
        "BOOL-C": {
            "active": true
        }
};

var app_count = 0;
for(var ik in items) {
    if(ik.indexOf("APP-") >= 0) {
        app_count++;
    }
}
alert(app_count);
console.log(app_count);

Comments

0

You can do something like this.

http://jsfiddle.net/098o3kch/1/

var items = { "APP-A": {
            "active": true
        },
        "APP-B": {
            "active": true
        },
        "APP-C": {
            "active": true
        },
        "BOOL-A": {
            "active": true
        },
        "BOOL-B": {
            "active": true
        },
        "BOOL-C": {
            "active": true
        }
};

function getCount(obj,pattern){
var cnt = 0;
 for(var item in obj)
{
if(item.indexOf(pattern) >= 0)  
cnt++;
}
return cnt;
 }

alert(getCount(items, "APP-"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.