javascript统计数组中各个元素的个数
describe('practice-2-3', function() {
var collection = [
"a", "a", "a",
"e", "e", "e", "e", "e", "e", "e",
"h", "h", "h", "h", "h", "h", "h[3]", "h", "h",
"t", "t-2", "t", "t", "t", "t", "t", "t", "t[10]",
"f", "f", "f", "f", "f", "f", "f", "f", "f",
"c:8",
"g", "g", "g", "g", "g", "g", "g",
"b", "b", "b", "b", "b", "b",
"d-5"
];
it("把A集合中相同的元素统计出数量", function() {
var result = count_same_elements(collection);
expect(result).toEqual([
{name: "a", summary: 3},
{name: "e", summary: 7},
{name: "h", summary: 11},
{name: "t", summary: 19},
{name: "f", summary: 9},
{name: "c", summary: 8},
{name: "g", summary: 7},
{name: "b", summary: 6},
{name: "d", summary: 5}
]);
});
});
//怎样统计d-5,c:8,t[10]这些元素的个数?需要用到什么方法?
var collection = [
"a", "a", "a",
"e", "e", "e", "e", "e", "e", "e",
"h", "h", "h", "h", "h", "h", "h[3]", "h", "h",
"t", "t-2", "t", "t", "t", "t", "t", "t", "t[10]",
"f", "f", "f", "f", "f", "f", "f", "f", "f",
"c:8",
"g", "g", "g", "g", "g", "g", "g",
"b", "b", "b", "b", "b", "b",
"d-5"
];
it("把A集合中相同的元素统计出数量", function() {
var result = count_same_elements(collection);
expect(result).toEqual([
{name: "a", summary: 3},
{name: "e", summary: 7},
{name: "h", summary: 11},
{name: "t", summary: 19},
{name: "f", summary: 9},
{name: "c", summary: 8},
{name: "g", summary: 7},
{name: "b", summary: 6},
{name: "d", summary: 5}
]);
});
});
//怎样统计d-5,c:8,t[10]这些元素的个数?需要用到什么方法?
...全文
相关推荐
js代码-数组:统计元素出现的个数(严格)
js代码-数组:统计元素出现的个数(严格)
JS统计数组元素个数[项目代码]
本文介绍了在JavaScript中如何统计数组中指定元素的个数。通过使用数组的filter()方法,可以筛选出符合条件的元素并生成新数组,进而通过新数组的长度来统计指定元素的个数。文章提供了详细的代码示例,展示了如何自定义函数countNum来实现这一功能,并演示了如何调用该函数来统计不同元素的个数。这种方法简单有效,适用于需要快速统计数组中特定元素出现次数的场景。
js代码-记录出现个数(三)
js代码-记录出现个数(三)
js 数组工具类 ArrayUtil
NULL 博文链接:https://871421448.iteye.com/blog/2148014
JS获取字符串型数组下标的数组长度的代码
标题页 [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]


