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

如何统计JSON列表项出现次数?Lodash汽车品牌统计优化咨询

Great question! Let's break this down clearly, starting with the general case of counting element occurrences in a JSON array, then diving into the optimized Lodash solution tailored to your car brand use case.


1. General: Counting Element Occurrences in a JSON Array

Lodash has a straightforward go-to method for this: _.countBy(). It works seamlessly for both simple value arrays and arrays of objects (where you want to count occurrences of a specific property).

Example for simple value arrays:

If your JSON has a list of primitive values (strings, numbers):

const jsonData = {"fruits": ["apple", "banana", "apple", "orange", "banana", "apple"]};
const fruitCounts = _.countBy(jsonData.fruits);
// Result: { apple: 3, banana: 2, orange: 1 }

Example for object arrays (counting a specific property):

For your car owner data, to count all brands present:

const carData = {"data": [{"ownerId":"00001", "make": "Honda"}, {"ownerId":"00002", "make": "Nissan"}, {"ownerId":"00003", "make": "Audi"}, {"ownerId":"00004", "make": "Porsche"}, {"ownerId":"00005", "make": "Honda"}, {"ownerId":"00006", "make": "Honda"}, {"ownerId":"00007", "make": "Audi"}, {"ownerId":"00008", "make": "Volkswagen"}, {"ownerId":"00009", "make": "Honda"}]};
const allBrandCounts = _.countBy(carData.data, 'make');
// Result: { Honda:4, Nissan:1, Audi:2, Porsche:1, Volkswagen:1 }

2. Optimized Lodash Solution for Predefined Brand Lists

When you only want to count brands from a predefined list (and ignore others), we can optimize for both readability and performance depending on your dataset size.

First, define your predefined brand list:

const predefinedMakes = ['Honda', 'Audi', 'Nissan', 'Toyota'];

Option 1: Simple & Readable (Small to Medium Datasets)

Combine _.filter() and _.countBy() to first narrow down to relevant items, then count:

const filteredBrandCounts = _.countBy(
  _.filter(carData.data, item => _.includes(predefinedMakes, item.make)),
  'make'
);
// Result: { Honda:4, Audi:2, Nissan:1 } (Toyota is excluded since it doesn't appear in the data)

This is easy to read and maintain, perfect for most everyday use cases.

Option 2: Single-Pass Optimization (Large Datasets)

For bigger datasets, use _.reduce() to handle filtering and counting in a single pass (avoids iterating over the array twice):

const optimizedBrandCounts = _.reduce(carData.data, (acc, item) => {
  const make = item.make;
  if (_.includes(predefinedMakes, make)) {
    acc[make] = (acc[make] || 0) + 1;
  }
  return acc;
}, {});
// Same result as Option 1, but with better performance for large arrays

Option 3: Include Predefined Brands with 0 Counts

If you need to show all predefined brands in the result (even those with 0 occurrences), initialize the accumulator with zeros first:

// Create an initial object with all predefined makes set to 0
const initialCounts = _.fromPairs(_.map(predefinedMakes, make => [make, 0]));

const completeBrandCounts = _.reduce(carData.data, (acc, item) => {
  const make = item.make;
  if (_.has(acc, make)) { // Check if the brand is in our predefined list
    acc[make]++;
  }
  return acc;
}, initialCounts);
// Result: { Honda:4, Audi:2, Nissan:1, Toyota:0 }

This is great for reporting where you need to display the full set of predefined brands regardless of presence in the data.


Hope these solutions fit your needs! If you need to handle edge cases (like null/undefined make values), just tweak the checks slightly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:35:51