简体   繁体   English

如何在AngularJS中获取值为x的项目的行数?

[英]How to get row count for a items with value x in AngularJS?

I have the following object 我有以下对象

    var countries = [
        {country: "Ireland", continent: "Europe", language: "English"},
        {country: "Spain", continent: "Europe", language: "Spanish"},
        {country: "United Kingdom", continent: "Europe", language: "English"},
        {country: "France", continent: "Europe", language: "French"},
        {country: "Germany", continent: "Europe", language: "(other)"},
        {country: "Sweden", continent: "Europe", language: "(other)"},
        {country: "Norway", continent: "Europe", language: "(other)"},
        {country: "Italy", continent: "Europe", language: "(other)"},
        {country: "Greece", continent: "Europe", language: "(other)"},
        {country: "Iceland", continent: "Europe", language: "(other)"},
        {country: "Portugal", continent: "Europe", language: "Portuguese"},
        {country: "Malta", continent: "Europe", language: "(other)"},
        {country: "Brazil", continent: "South America", language: "Portuguese"},
        {country: "Argentina", continent: "South America", language: "Spanish"},
        {country: "Colombia", continent: "South America", language: "Spanish"},
        {country: "Peru", continent: "South America", language: "Spanish"},
        {country: "Venezuela", continent: "South America", language: "Spanish"},
        {country: "Uruguay", continent: "South America", language: "Spanish"}
    ];

How can I count the objects with for example continent === 'South America' ? 如何计算continent ==='South America'的对象? It is a JSON list of obejcts returned from a WebService within an AngularJS Controller. 它是从AngularJS控制器中的Web服务返回的对象的JSON列表。

You can use Array#reduce() 您可以使用Array#reduce()

 var countries = [{"country":"Ireland","continent":"Europe","language":"English"},{"country":"Spain","continent":"Europe","language":"Spanish"},{"country":"United Kingdom","continent":"Europe","language":"English"},{"country":"France","continent":"Europe","language":"French"},{"country":"Germany","continent":"Europe","language":"(other)"},{"country":"Sweden","continent":"Europe","language":"(other)"},{"country":"Norway","continent":"Europe","language":"(other)"},{"country":"Italy","continent":"Europe","language":"(other)"},{"country":"Greece","continent":"Europe","language":"(other)"},{"country":"Iceland","continent":"Europe","language":"(other)"},{"country":"Portugal","continent":"Europe","language":"Portuguese"},{"country":"Malta","continent":"Europe","language":"(other)"},{"country":"Brazil","continent":"South America","language":"Portuguese"},{"country":"Argentina","continent":"South America","language":"Spanish"},{"country":"Colombia","continent":"South America","language":"Spanish"},{"country":"Peru","continent":"South America","language":"Spanish"},{"country":"Venezuela","continent":"South America","language":"Spanish"},{"country":"Uruguay","continent":"South America","language":"Spanish"}] var result = countries.reduce(function(r, e) { if(e.continent === 'South America') r++ return r; }, 0); console.log(result) 

You can create an array, like : 您可以创建一个数组,例如:

var southAfrikaResult = []

Then you loop on your countries array (with angular.forEach) and check if 然后您循环使用国家数组(使用angular.forEach)并检查是否

item.continent === 'South Afika'

If so, then push item into your southAfricaResult. 如果是这样,则将项目推入您的southAfricaResult。 Your final result will be : 您的最终结果将是:

var your_result = southAfricaResult.length;

Edit : the filter solution proposed by Jose Hermosilla Rodrigo it way better. 编辑:Jose Hermosilla Rodrigo提出的过滤器解决方案更好。

I would do sth like 我会做某事

var countries = [
    {country: "Ireland", continent: "Europe", language: "English"},
    {country: "Spain", continent: "Europe", language: "Spanish"},
    {country: "United Kingdom", continent: "Europe", language: "English"},
    {country: "France", continent: "Europe", language: "French"},
    {country: "Germany", continent: "Europe", language: "(other)"},
    {country: "Sweden", continent: "Europe", language: "(other)"},
    {country: "Norway", continent: "Europe", language: "(other)"},
    {country: "Italy", continent: "Europe", language: "(other)"},
    {country: "Greece", continent: "Europe", language: "(other)"},
    {country: "Iceland", continent: "Europe", language: "(other)"},
    {country: "Portugal", continent: "Europe", language: "Portuguese"},
    {country: "Malta", continent: "Europe", language: "(other)"},
    {country: "Brazil", continent: "South America", language: "Portuguese"},
    {country: "Argentina", continent: "South America", language: "Spanish"},
    {country: "Colombia", continent: "South America", language: "Spanish"},
    {country: "Peru", continent: "South America", language: "Spanish"},
    {country: "Venezuela", continent: "South America", language: "Spanish"},
    {country: "Uruguay", continent: "South America", language: "Spanish"}
];

j = 0;
newCountry = [];    

for(i=0; i<countries.length; i++)
{
    if(countries[i]['continent'] == 'South America')
    { 
        j++; //only for size
        newCountry.push(countries[i]);
    }
}

Thanks that you have only number of countries but in newCountry you will have array with countries in that continent and then you can use newCountry.length and you will get same result. 感谢您只有几个国家/地区,但是在newCountry中,您将拥有该大陆上的国家/地区数组,然后可以使用newCountry.length,您将获得相同的结果。

you can create array like this 你可以像这样创建数组

var result = [];

You angular.foreach loop 你angular.foreach循环

angular.forEach(countries, function(value, key) {
if(value.continent == "South America")
       //your logic
  result.push(value.continent);
});

finally you count the array length 最后你计算数组长度

var count = result.length;

If you want to get to more detail in any step, Click here ! 如果要在任何步骤中获取更多详细信息,请单击此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM