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

数组映射合并需求:将会话消息数组与参与者数组合并为目标数组

实现逻辑

两个数组的同索引元素对应同一个会话,按索引配对组装即可实现合并。

代码示例

JavaScript 实现

// 前置校验:保证两个数组长度匹配,避免索引异常
if (messageArray.length !== participantArray.length) {
  throw new Error('消息数组与参与者数组长度不匹配,无法完成合并')
}

const mergedArray = messageArray.map((msgItem, index) => {
  // 取当前会话的最后一条消息、对应参与者数组组装成目标结构
  return [msgItem[0], participantArray[index]]
})

Python 实现

if len(message_array) != len(participant_array):
    raise ValueError("消息数组与参与者数组长度不匹配,无法完成合并")

merged_array = [ [msg[0], part] for msg, part in zip(message_array, participant_array) ]

提示:如果实际业务中消息子数组的最后一个元素才是最后一条消息,可将上述代码中的msgItem[0]替换为msgItem.at(-1)(JS)或msg[-1](Python)适配场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 20:09:03