在JavaScript开发场景中,经常需要对存储了多个单词的数组进行统计,得到每个单词出现的次数,并且将结果整理成对象数组的形式返回,方便后续的数据处理和展示。

实现思路分析
要实现统计数组中单词出现次数并返回对象数组的功能,整体可以分为两个核心步骤:
- 第一步:遍历目标数组,统计每个单词的出现次数,将结果存储在一个普通的对象中,对象的键是单词,值是对应的出现次数。
- 第二步:将统计得到的普通对象转换为对象数组,每个数组元素是一个包含单词和对应次数的对象。
在这个过程中还需要注意一些边界情况,比如数组为空、数组中存在大小写不同的相同单词、数组中存在非字符串元素等,需要根据实际需求做对应的处理。
基础实现代码
下面是一个最基础的实现版本,假设数组中的元素都是字符串,且不需要处理大小写差异:
// 定义待统计的单词数组
const wordArray = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple'];
// 统计单词出现次数的函数
function countWordsToObjectArray(arr) {
// 处理空数组的情况
if (!Array.isArray(arr) || arr.length === 0) {
return [];
}
// 第一步:统计每个单词的出现次数
const countMap = {};
for (let i = 0; i < arr.length; i++) {
const word = arr[i];
// 跳过非字符串元素
if (typeof word !== 'string') {
continue;
}
if (countMap[word]) {
countMap[word] += 1;
} else {
countMap[word] = 1;
}
}
// 第二步:将统计对象转换为对象数组
const resultArray = [];
for (const key in countMap) {
if (countMap.hasOwnProperty(key)) {
resultArray.push({
word: key,
count: countMap[key]
});
}
}
return resultArray;
}
// 调用函数并输出结果
const result = countWordsToObjectArray(wordArray);
console.log(result);
// 输出:[{word: 'apple', count: 3}, {word: 'banana', count: 2}, {word: 'orange', count: 1}]
优化版本:处理大小写和特殊字符
实际场景中,可能需要忽略单词的大小写差异,比如将Apple和apple视为同一个单词,同时可能需要过滤掉空字符串等无效元素,下面是优化后的实现:
function countWordsToObjectArrayOptimized(arr, ignoreCase = true) {
if (!Array.isArray(arr) || arr.length === 0) {
return [];
}
const countMap = {};
for (let i = 0; i < arr.length; i++) {
let word = arr[i];
// 跳过非字符串或者空字符串
if (typeof word !== 'string' || word.trim() === '') {
continue;
}
// 如果需要忽略大小写,统一转为小写
if (ignoreCase) {
word = word.toLowerCase();
}
if (countMap[word]) {
countMap[word] += 1;
} else {
countMap[word] = 1;
}
}
const resultArray = [];
for (const key in countMap) {
if (countMap.hasOwnProperty(key)) {
resultArray.push({
word: key,
count: countMap[key]
});
}
}
return resultArray;
}
// 测试优化后的函数
const testArray = ['Apple', 'banana', 'apple', '', 'Orange', 'banana', 123];
const optimizedResult = countWordsToObjectArrayOptimized(testArray);
console.log(optimizedResult);
// 输出:[{word: 'apple', count: 2}, {word: 'banana', count: 2}, {word: 'orange', count: 1}]
使用reduce方法简化代码
JavaScript的数组reduce方法可以更简洁地实现统计逻辑,下面是使用reduce的实现方式:
function countWordsByReduce(arr, ignoreCase = true) {
if (!Array.isArray(arr) || arr.length === 0) {
return [];
}
// 使用reduce统计单词次数
const countMap = arr.reduce((map, item) => {
if (typeof item !== 'string' || item.trim() === '') {
return map;
}
let word = ignoreCase ? item.toLowerCase() : item;
map[word] = (map[word] || 0) + 1;
return map;
}, {});
// 转换为对象数组
return Object.keys(countMap).map(key => ({
word: key,
count: countMap[key]
}));
}
// 测试reduce版本
const reduceResult = countWordsByReduce(['hello', 'world', 'hello', 'Hello']);
console.log(reduceResult);
// 输出:[{word: 'hello', count: 3}, {word: 'world', count: 1}]
常见问题说明
为什么统计对象要转成对象数组
普通的对象虽然可以存储单词和次数的对应关系,但是在很多场景下,比如需要按顺序遍历、需要传递给某些只支持数组格式的接口时,对象数组的格式会更方便使用。
如何处理数组中的重复空格单词
如果数组中存在多个空格组成的字符串,可以在统计前先调用trim方法过滤,或者在判断时跳过空字符串,避免统计无效内容。
函数的时间复杂度是多少
上述实现的时间复杂度都是O(n),其中n是输入数组的长度,因为只需要遍历数组一次完成统计,再遍历一次统计对象完成转换,整体是线性时间复杂度,性能表现较好。
数组统计单词计数对象数组JavaScript修改时间:2026-06-25 18:39:28