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

Watson Assistant:移除数组中的重复项

在Watson Assistant中移除数组重复项的方法

核心思路

利用JavaScript的Set对象自动过滤重复值的特性,快速实现数组去重。

操作步骤

  • 在Watson Assistant的对话节点中,进入自定义操作模块(或直接在上下文变量的计算字段中编写代码)
  • 针对目标数组(假设原数组存储在上下文变量$fruitList中),写入以下代码完成去重:
    // 从上下文获取原数组,通过Set去重后转回数组
    const uniqueFruits = [...new Set($fruitList)];
    // 将去重结果存入新的上下文变量,也可直接覆盖原变量
    context.set("uniqueFruitList", uniqueFruits);
    
  • 保存配置后测试,$uniqueFruitList的值即为去重后的数组:["Apple","Orange","Banana","cherries"]

补充说明(大小写不敏感去重)

如果需要忽略大小写差异(比如将"Apple"和"apple"视为同一值),可以先统一转换大小写再处理:

// 先转小写去重,再恢复首字母大写格式
const uniqueFruits = [...new Set($fruitList.map(item => item.toLowerCase()))]
  .map(item => item.charAt(0).toUpperCase() + item.slice(1));
context.set("uniqueFruitList", uniqueFruits);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 00:21:41