You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何统计API返回JSON数组中DeptName的出现次数并生成键值对?

Counting DeptName Occurrences in a JSON Array (Lowercase Keys)

Hey there! Let's break down how to count the occurrences of each DeptName in your array and output the result with lowercase keys, just like you want. Here are a couple of simple, effective ways to do this in JavaScript:

Method 1: Use reduce() (Clean & Concise)

The reduce() method is perfect for accumulating values into an object—this is the most idiomatic approach for this kind of counting task:

const data = [ 
  {"id":"1939317721","pauseReason":"DISPLAY","DeptName":"Account"}, 
  {"id":"1939317722","pauseReason":"DISPLAY","DeptName":"Admission"}, 
  {"id":"1939317723","pauseReason":"DISPLAY","DeptName":"Account"}, 
  {"id":"1939317724","pauseReason":"DISPLAY","DeptName":"Account"}, 
];

const res = data.reduce((counts, item) => {
  // Convert DeptName to lowercase to match your expected output format
  const dept = item.DeptName.toLowerCase();
  // Increment the count: if the dept doesn't exist yet, default to 0 before adding 1
  counts[dept] = (counts[dept] || 0) + 1;
  return counts;
}, {}); // Start with an empty object to store counts

console.log(res); // Output: { account: 3, admission: 1 }

How this works:

  • We initialize reduce() with an empty object ({}) as the starting value for our counts.
  • For each item in the array, we convert DeptName to lowercase to ensure consistent keys (since your desired result uses lowercase).
  • We check if the department already exists in our counts object: if it does, we increment its value; if not, we set it to 1 (using (counts[dept] || 0) to handle the "first occurrence" case).

Method 2: Use forEach() (More Explicit for Beginners)

If you prefer a more straightforward, step-by-step approach, a forEach() loop makes the counting logic easy to follow:

const data = [ 
  {"id":"1939317721","pauseReason":"DISPLAY","DeptName":"Account"}, 
  {"id":"1939317722","pauseReason":"DISPLAY","DeptName":"Admission"}, 
  {"id":"1939317723","pauseReason":"DISPLAY","DeptName":"Account"}, 
  {"id":"1939317724","pauseReason":"DISPLAY","DeptName":"Account"}, 
];

const res = {}; // Empty object to hold our counts

data.forEach(item => {
  const dept = item.DeptName.toLowerCase();
  // Check if the department is already in our counts object
  if (res[dept]) {
    res[dept]++; // Increment if it exists
  } else {
    res[dept] = 1; // Set to 1 if it's the first time we see it
  }
});

console.log(res); // Output: { account: 3, admission: 1 }

Bonus: Add Robustness (Handle Missing DeptName)

If your array might contain items without a DeptName field, add a quick check to avoid errors:

// For reduce()
const res = data.reduce((counts, item) => {
  if (item.DeptName) { // Only process if the field exists
    const dept = item.DeptName.toLowerCase();
    counts[dept] = (counts[dept] || 0) + 1;
  }
  return counts;
}, {});

// For forEach()
data.forEach(item => {
  if (item.DeptName) {
    const dept = item.DeptName.toLowerCase();
    res[dept] = (res[dept] || 0) + 1;
  }
});

This ensures your code won't throw errors if any entries are missing the DeptName property.

内容的提问来源于stack exchange,提问作者shivani joshi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:02:29