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

使用UnderscoreJS按条件修剪对象数组的实现问题

Trim Leading/Trailing Empty-Name Entries with Underscore.js

Got it, let's solve this problem where we need to trim only the leading and trailing consecutive entries with empty name values from your array, while keeping any middle empty name entries intact. Here's how to complete your Underscore.js implementation:

Step-by-Step Solution

First, let's recap your goal: we want to turn this input array:

var data = [ 
  {name: '', age: 12}, 
  {name: 'bcd', age: 15}, 
  {name: 'cdf', age: 13}, 
  {name: '', age: 11}, 
  {name: 'fgh', age: 8}, 
  {name: '', age: 10}, 
];

into this trimmed result:

[{name: 'bcd', age: 15}, {name: 'cdf', age: 13}, {name: '', age: 11}, {name: 'fgh', age: 8}]

1. Find the first non-empty name entry

You already started this part correctly—we use _.find to get the first entry with a non-empty name, then get its index with _.indexOf:

const firstNonEmpty = _.find(data, d => d.name !== '');

2. Find the last non-empty name entry

To get the end of our trimmed array, we use _.findLast (Underscore's method for searching from the end of the array) to grab the last non-empty name entry, then get its index:

const lastNonEmpty = _.findLast(data, d => d.name !== '');

3. Handle edge cases & slice the array

We need to account for cases where the array has no non-empty entries (return an empty array). Otherwise, use _.slice to extract the range from the first non-empty index to one past the last non-empty index (since slice excludes the end index):

let trimmedData;

if (!firstNonEmpty || !lastNonEmpty) {
  trimmedData = []; // No valid entries to keep
} else {
  const firstIndex = _.indexOf(data, firstNonEmpty);
  const lastIndex = _.indexOf(data, lastNonEmpty);
  trimmedData = _.slice(data, firstIndex, lastIndex + 1);
}

Full Working Code

Putting it all together:

var data = [ 
  {name: '', age: 12}, 
  {name: 'bcd', age: 15}, 
  {name: 'cdf', age: 13}, 
  {name: '', age: 11}, 
  {name: 'fgh', age: 8}, 
  {name: '', age: 10}, 
];

const firstNonEmpty = _.find(data, d => d.name !== '');
const lastNonEmpty = _.findLast(data, d => d.name !== '');

let trimmedData;
if (!firstNonEmpty || !lastNonEmpty) {
  trimmedData = [];
} else {
  const firstIndex = _.indexOf(data, firstNonEmpty);
  const lastIndex = _.indexOf(data, lastNonEmpty);
  trimmedData = _.slice(data, firstIndex, lastIndex + 1);
}

console.log(trimmedData);
// Output: [{name: 'bcd', age: 15}, {name: 'cdf', age: 13}, {name: '', age: 11}, {name: 'fgh', age: 8}]

Alternative Concise Approach

If you prefer a more compact version, you can collect all indices of non-empty entries, then use the min and max indices to slice:

const nonEmptyIndices = _.map(_.filter(data, d => d.name !== ''), item => _.indexOf(data, item));
const trimmedData = nonEmptyIndices.length === 0 
  ? [] 
  : _.slice(data, _.min(nonEmptyIndices), _.max(nonEmptyIndices) + 1);

This works because we're only removing the leading/trailing empty entries—any middle empty entries are between the first and last non-empty indices, so they get included in the slice.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:17:26