gpt4 book ai didi

javascript - 如何计算 Angular 或javascript中分组数组的长度?

转载 作者:行者123 更新时间:2023-11-29 20:54:30 25 4
gpt4 key购买 nike

array = [  
{
name: 'hello 1',
checked: true,
color:'#1ac3ec',
group:'first category'
},
{
name: 'hello 2',
checked: true,
color:'#7dc55c',
group:'first category'
},
{
name: 'hello 3',
checked: true,
color:'#005073',
group:'second category'
},{
name: 'hello 4',
checked: true,
color:'#fbb330',
group:'second category'
}
]

我想得到每组的长度。我们怎样才能得到它?

例如 array.group['second category'].length

最佳答案

你可以使用 array.filter:

var array = [  
{
name: 'hello 1',
checked: true,
color:'#1ac3ec',
group:'first category'
},
{
name: 'hello 2',
checked: true,
color:'#7dc55c',
group:'first category'
},
{
name: 'hello 3',
checked: true,
color:'#005073',
group:'second category'
},{
name: 'hello 4',
checked: true,
color:'#fbb330',
group:'second category'
}
];

var len1 = array.filter(e => e.group === 'first category').length;
console.log(len1);
var len2 = array.filter(e => e.group === 'second category').length;
console.log(len2);

关于javascript - 如何计算 Angular 或javascript中分组数组的长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50016070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com