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

请求协助:将数组数据分类为Tier1/Tier2对应4组数组

How to Split Unstructured Array into 8 Target Arrays (Tier1/Tier2, Left/Right, Start/End)

Got it, let's work through this together. Since you didn't share the exact structure of your source array, I'll use a common real-world example to demonstrate the approach—you can tweak the logic to match your actual data format.


First, let's define a sample source array

Let's assume your unstructured data is an array of objects with properties that map to the tier, side, and phase (start/end) we need to filter by:

const sourceArray = [
  { tier: 1, side: 'left', phase: 'start', data: 'item1' },
  { tier: 1, side: 'left', phase: 'end', data: 'item2' },
  { tier: 1, side: 'right', phase: 'start', data: 'item3' },
  { tier: 1, side: 'right', phase: 'end', data: 'item4' },
  { tier: 2, side: 'left', phase: 'start', data: 'item5' },
  { tier: 2, side: 'left', phase: 'end', data: 'item6' },
  { tier: 2, side: 'right', phase: 'start', data: 'item7' },
  { tier: 2, side: 'right', phase: 'end', data: 'item8' },
  // ... more uncategorized items
];

Option 1: Straightforward filtering (easy to read)

Use JavaScript's Array.filter() method to extract items matching each combination of tier, side, and phase. This is simple and explicit:

// Tier 1 groups
const tier1LeftStart = sourceArray.filter(item => 
  item.tier === 1 && item.side === 'left' && item.phase === 'start'
);
const tier1LeftEnd = sourceArray.filter(item => 
  item.tier === 1 && item.side === 'left' && item.phase === 'end'
);
const tier1RightStart = sourceArray.filter(item => 
  item.tier === 1 && item.side === 'right' && item.phase === 'start'
);
const tier1RightEnd = sourceArray.filter(item => 
  item.tier === 1 && item.side === 'right' && item.phase === 'end'
);

// Tier 2 groups
const tier2LeftStart = sourceArray.filter(item => 
  item.tier === 2 && item.side === 'left' && item.phase === 'start'
);
const tier2LeftEnd = sourceArray.filter(item => 
  item.tier === 2 && item.side === 'left' && item.phase === 'end'
);
const tier2RightStart = sourceArray.filter(item => 
  item.tier === 2 && item.side === 'right' && item.phase === 'start'
);
const tier2RightEnd = sourceArray.filter(item => 
  item.tier === 2 && item.side === 'right' && item.phase === 'end'
);

Option 2: DRY (Don't Repeat Yourself) helper function

If you want to avoid duplicating code, create a reusable helper function to handle the filtering:

function filterArrayByCriteria(source, tier, side, phase) {
  return source.filter(item => 
    item.tier === tier && item.side === side && item.phase === phase
  );
}

// Generate all 8 arrays with a single function call each
const tier1LeftStart = filterArrayByCriteria(sourceArray, 1, 'left', 'start');
const tier1LeftEnd = filterArrayByCriteria(sourceArray, 1, 'left', 'end');
const tier1RightStart = filterArrayByCriteria(sourceArray, 1, 'right', 'start');
const tier1RightEnd = filterArrayByCriteria(sourceArray, 1, 'right', 'end');

const tier2LeftStart = filterArrayByCriteria(sourceArray, 2, 'left', 'start');
const tier2LeftEnd = filterArrayByCriteria(sourceArray, 2, 'left', 'end');
const tier2RightStart = filterArrayByCriteria(sourceArray, 2, 'right', 'start');
const tier2RightEnd = filterArrayByCriteria(sourceArray, 2, 'right', 'end');

If your source array is plain values (not objects)

If your data is a flat array of strings (e.g., ["T1-L-S-item1", "T1-L-E-item2", ...]), adjust the filtering to parse each item's prefix:

const sourceArray = ["T1-L-S-item1", "T1-L-E-item2", "T1-R-S-item3", "T2-L-S-item5", ...];

const tier1LeftStart = sourceArray.filter(item => item.startsWith("T1-L-S"));
const tier1LeftEnd = sourceArray.filter(item => item.startsWith("T1-L-E"));
// Repeat this pattern for the remaining 6 arrays, updating the prefix each time

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 07:50:36