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

如何从sql.js返回的查询结果中提取指定列为独立数组

实现代码

最简写法(适配你当前的返回结构)

你当前查询返回的字段顺序固定为time_在前、temp在后,直接按索引取值即可:

const queryResult = res[0];
const labels = queryResult.values.map(row => row[0]);
const data = queryResult.values.map(row => row[1]);

通用写法(不依赖字段返回顺序)

如果后续需要调整select语句的字段顺序,建议先匹配字段名拿到对应索引,避免逻辑出错:

const queryResult = res[0];
// 先匹配字段名获取对应下标
const timeColIndex = queryResult.columns.indexOf('time_');
const tempColIndex = queryResult.columns.indexOf('temp');

const labels = queryResult.values.map(row => row[timeColIndex]);
const data = queryResult.values.map(row => row[tempColIndex]);

运行后得到的labels和data就是符合要求的数组格式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 10:45:03