【问题标题】:Angular: How to get the count of the Object with specific value?Angular:如何获取具有特定值的对象的计数?
【发布时间】:2017-07-27 21:06:44
【问题描述】:

我有一个带有对象的 JSON 数据库。每个属性都有一个特定的赋值:a、b 或 c。

[
  {
    "id": 1,
    "category": "a"
  },
  {
    "id": 2,
    "category": "b"
  },
  {
    "id": 3,
    "category": "c"
  },
  {
    "id": 4,
    "category": "a"
  },
  {
    "id": 5,
    "category": "a"
  },
  {
    "id": 5,
    "category": "b"
  }
]

我想显示如下内容:

总共有 6 个项目:a x 3、b x 2 和 c x 1。 p>

我知道我必须使用objectsinmyjsondatabase.length 才能得到总数。

我想知道如何获得具有特定值的对象的长度(数量)?

【问题讨论】:

  • 如果您想在视图中进行计数,answer 将为您提供帮助。

标签: javascript


【解决方案1】:

定义一个函数:

getCount(character) {
  return this.objects.filter(obj => obj.category === character).length;
}

并将其称为: this.getCount('a');

【讨论】:

  • 我需要在哪里声明这个函数?在我的组件或服务中?
  • @jonathan 我认为这应该写在组件中。服务应该将数据按原样传递给组件。但是,是的,您也可以在服务中进行过滤。
【解决方案2】:

解决问题的一种方法是使用 map-reduce。这是一个快速的解决方案。希望能解决您的问题。

var data = [
  {
    "id": 1,
    "category": "a"
  },
  {
    "id": 2,
    "category": "b"
  },
  {
    "id": 3,
    "category": "c"
  },
  {
    "id": 4,
    "category": "a"
  },
  {
    "id": 5,
    "category": "a"
  },
  {
    "id": 5,
    "category": "b"
  }
];

// First get list of all categories
var categories = data.map(function(x) { return x["category"]; });

// Count no of items in each category
var countByCategories = categories.reduce(function(x, y) {
    if (typeof x !== "object") {
       var reduced = {};
       reduced[x] = 1;
       reduced[y] = 1;
       return reduced;
    }
    
    x[y] = (x[y] || 0) + 1;
    return x;
});

// Final build string that you want
var categoryStrings = [];
for (var category in countByCategories) {
   categoryStrings.push(category + ' x ' + countByCategories[category]); 
}

var msg = 'There is a total of ' + categories.length + ' items: ';
if (categoryStrings.length > 2) {
   msg = categoryStrings.slice(0, -1).join(', '); 
   msg += ' and ' + categoryStrings.slice(-1);
} else {
   msg = categoryStrings.join(', '); 
}

// print results
console.log(msg);

【讨论】:

    【解决方案3】:

    您可以轻松计算具有此类特定价值的对象

    let objects = [
        {
            "id": 1,
            "category": "a"
        },
        {
            "id": 2,
            "category": "b"
        },
        {
            "id": 3,
            "category": "c"
        },
        {
            "id": 4,
            "category": "a"
        },
        {
            "id": 5,
            "category": "a"
        },
        {
            "id": 5,
            "category": "b"
        }
    ];
    var filtered = new Array();
    objects.filter(function (t) {
        var found = filtered.some(function (el, index) {
            if (false == (t.category === el.category)) {
                return false;
            }
            filtered[index].count = filtered[index].count + 1;
            return true;
    
        }, filtered);
        if (false === found) {
            filtered.push({category: t.category, count: 1});
        }
    }, filtered);
    console.log('There is a total of ' + objects.length + ' items: '
        + filtered.map(function (t) {
            return t.category + ' x ' + t.count;
        })
    );

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-20
      • 2019-12-17
      • 1970-01-01
      • 2017-08-22
      • 1970-01-01
      相关资源
      最近更新 更多