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

如何从JSON数据指定部分提取内容生成独立数组?

解决方案

假设你已经通过API获取到目标JSON数据,以下是直接提取所需字段生成三个独立数组的JavaScript代码:

步骤1:解析JSON数据(若返回的是字符串格式)

如果API返回的是JSON字符串,先将其转换为JavaScript对象:

// apiResponse 为API返回的JSON字符串
const responseData = JSON.parse(apiResponse);
// 取出sharedEmotes数组
const sharedEmotes = responseData.sharedEmotes;

步骤2:提取字段生成数组

使用map方法遍历数组,分别提取每个元素的code、id、imageType字段:

// 提取表情代码数组
const emotecodes = sharedEmotes.map(emote => emote.code);
// 提取表情ID数组
const emoteids = sharedEmotes.map(emote => emote.id);
// 提取表情文件类型数组
const emotefiletypes = sharedEmotes.map(emote => emote.imageType);

map方法会遍历sharedEmotes中的每一个元素,返回你指定的字段值,最终自动组合成新的数组。如果sharedEmotes为空数组,生成的三个数组也会是空数组,不会报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 16:03:21