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

如何将含tag属性的JSON对象数组转换为单引号包裹的元素列表

实现方案

核心逻辑:遍历原数组提取所有tag属性值,给每个值包裹单引号后,用,\n作为分隔符拼接即可得到你需要的格式。

JavaScript 实现

const originArr = [
  { "tag": "search" },
  { "tag": "test" },
  { "tag": "css" }
];
// 遍历提取tag并拼接
const res = originArr.map(item => `'${item.tag}'`).join(',\n');
console.log(res);

运行后输出完全符合你要求的格式:

'search',
'test',
'css'

Python 实现

origin_arr = [
  {"tag": "search"},
  {"tag": "test"},
  {"tag": "css"}
]
# 生成器表达式处理后拼接
res = ',\n'.join(f"'{item['tag']}'" for item in origin_arr)
print(res)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 23:54:06