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

如何拆分服务端返回的动态字符串,将各属性值存入独立JS变量

实现方案

你可以直接用JavaScript内置的Array.map()方法批量提取属性,代码可以自适应数组内的对象数量,不需要做硬编码调整。

代码示例

如果已经拿到解析完成的服务端数组:

// 此处的serverData就是你从服务端获取的数组
const serverData = [{text :'AC Maintenance today ', value:16.00, color: '#DEE3E8'},{text :'test3', value:150.00, color: '#5A0C55'}]

const text = serverData.map(item => item.text)
const value = serverData.map(item => item.value)
const color = serverData.map(item => item.color)

如果服务端返回的是JSON字符串,需要先解析再提取:

// 此处为服务端返回的原始字符串
const rawResponseStr = `[{"text":"AC Maintenance today ","value":16.00,"color":"#DEE3E8"},{"text":"test3","value":150.00,"color":"#5A0C55"}]`
// 先解析字符串为JS数组
const serverData = JSON.parse(rawResponseStr)

const text = serverData.map(item => item.text)
const value = serverData.map(item => item.value)
const color = serverData.map(item => item.color)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 07:06:07