计算 JavaScript 对象中特定值的频率

发布于 2025-01-11 20:29:39 字数 363 浏览 1 评论 0原文

我有一个结构如下的 JavaScript 对象:

var subject = {all: "inactive",firstSingular: "active", secondarySingular: "inactive",thirdSingular: "active",firstPlural: "inactive", secondaryPlural: "inactive",thirdPlural: "inactive"

我想计算该对象内“active”值的实例数(即返回2)。我当然可以编写一个函数来迭代对象并计算值,尽管我想知道是否有更简洁的方法在 JavaScript 中执行此操作(在 1 行中),类似于 python 中的 collections.Counter 函数。

I have a JavaScript object that is structured as such:

var subjects = {all: "inactive", firstSingular: "active", secondSingular: "inactive", thirdSingular: "active", firstPlural: "inactive", secondPlural: "inactive", thirdPlural: "inactive"

I would like to count the instances of the "active" value within this object (i.e return 2). I could certainly write a function that iterates through the object and counts the values, though I was wondering if there was a cleaner way to do this (in 1 line) in JavaScript, similar to the collections.Counter function in python.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ペ泪落弦音 2025-01-18 20:29:39

使用 Object#valuesArray#reduce

const subjects = { all: "inactive", firstSingular: "active", secondSingular: "inactive", thirdSingular: "active", firstPlural: "inactive", secondPlural: "inactive", thirdPlural: "inactive" };

const count = Object.values(subjects).reduce((total, value) => value === 'active' ? total + 1 : total, 0);

console.log(count);

使用 Array#filter< 的另一个解决方案/code>而不是减少:

const subjects = { all: "inactive", firstSingular: "active", secondSingular: "inactive", thirdSingular: "active", firstPlural: "inactive", secondPlural: "inactive", thirdPlural: "inactive" };

const count = Object.values(subjects).filter(value => value === 'active').length;

console.log(count);

Using Object#values and Array#reduce:

const subjects = { all: "inactive", firstSingular: "active", secondSingular: "inactive", thirdSingular: "active", firstPlural: "inactive", secondPlural: "inactive", thirdPlural: "inactive" };

const count = Object.values(subjects).reduce((total, value) => value === 'active' ? total + 1 : total, 0);

console.log(count);

Another solution using Array#filter instead of reduce:

const subjects = { all: "inactive", firstSingular: "active", secondSingular: "inactive", thirdSingular: "active", firstPlural: "inactive", secondPlural: "inactive", thirdPlural: "inactive" };

const count = Object.values(subjects).filter(value => value === 'active').length;

console.log(count);

此岸叶落 2025-01-18 20:29:39

另一种可能的解决方案是使用 filter 并获取结果的长度

var subjects = {all: "inactive", firstSingular: "active",secondSingular:"inactive", thirdSingular: "active", firstPlural: "inactive",secondPlural: "inactive", thirdPlural: "inactive"}

var res = Object.values(subjects).filter((val) => val === 'active').length

console.log(res)

Another possible solution using filter and getting length of result

var subjects = {all: "inactive", firstSingular: "active",secondSingular:"inactive", thirdSingular: "active", firstPlural: "inactive",secondPlural: "inactive", thirdPlural: "inactive"}

var res = Object.values(subjects).filter((val) => val === 'active').length

console.log(res)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文