请求协助:将数组数据分类为Tier1/Tier2对应4组数组
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

