> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # Workflow\.map() `.map()` 方法會將上一個步驟的輸出資料映射至後續步驟的輸入,讓你能在步驟之間轉換資料。 ## 使用範例 ```typescript workflow.map(async ({ inputData }) => `${inputData.value} - map`) ``` ## 參數 **mappingFunction** (`(params: { inputData: any }) => any`): 轉換輸入資料並傳回映射結果的函式 ## 傳回值 **workflow** (`Workflow`): 用於方法鏈結的 Workflow 個體 ## 使用 `inputData` 使用 `inputData` 存取上一個步驟的完整輸出。 ```typescript .then(step1) .map(({ inputData }) => { console.log(inputData); }) ``` ## 使用 `getStepResult()` 參照特定步驟的個體,並使用 `getStepResult()` 存取該步驟的完整輸出。 ```typescript .then(step1) .map(async ({ getStepResult }) => { console.log(getStepResult(step1)); }) ``` ## 使用 `getInitData()` 使用 `getInitData()` 存取提供給 Workflow 的初始輸入資料。 ```typescript .then(step1) .map(async ({ getInitData }) => { console.log(getInitData()); }) ``` ## 使用 `mapVariable()` `.map()` 的物件形式提供另一種宣告式語法來映射欄位。你無需編寫函式,而是定義一個物件,其中每個鍵都是新的欄位名稱,而每個值則使用 `mapVariable()` 從先前的步驟或 Workflow 輸入中擷取資料。 從 workflows 模組匯入 `mapVariable()`: ```typescript import { mapVariable } from '@mastra/core/workflows' ``` ### 從步驟輸出中擷取欄位 配合 `step` 使用 `mapVariable()`,從步驟輸出中擷取特定欄位,並將其映射至新的欄位名稱。`path` 參數指定要擷取的欄位。 在此範例中,會從 `step1` 的輸出中擷取 `value` 欄位,並將其映射至名為 `details` 的新欄位: ```typescript .then(step1) .map({ details: mapVariable({ step: step1, path: "value" }) }) ``` ### 從 Workflow 輸入中擷取欄位 配合 `initData` 使用 `mapVariable()`,從 Workflow 的初始輸入資料中擷取特定欄位。當你需要將原始 Workflow 輸入傳遞至後續步驟時,這種方式十分有用。 在此範例中,會從 Workflow 輸入中擷取 `value` 欄位,並將其映射至名為 `details` 的欄位: ```typescript export const testWorkflow = createWorkflow({...}); testWorkflow .then(step1) .map({ details: mapVariable({ initData: testWorkflow, path: "value" }) }) ``` ## 相關內容 - [輸入資料映射](https://mastra.zisheng.pro/zh-HK/docs/workflows/control-flow)