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

如何将多个值转为单个数组并获取最后一个值(如2022-2023)

解决方案

1. 收集值到数组

在回调函数里初始化一个空数组,遍历fieldData.rows时把每个row.qText推入数组即可:

fieldData.OnData.bind(function(){
    // 初始化空数组存储所有qText值
    const textArray = [];
    
    fieldData.rows.forEach(function(row){
        textArray.push(row.qText);
        console.log(row.qText); // 保留原日志输出
    });

    // 此时textArray就是存储所有值的可用数组
    console.log('所有值的数组:', textArray);

    // 2. 获取数组最后一个值
    if (textArray.length > 0) {
        // 方法一:通过数组长度-1索引获取
        const lastValue = textArray[textArray.length - 1];
        // 方法二:ES2022+可用的at方法,写法更简洁
        // const lastValue = textArray.at(-1);
        
        console.log('数组最后一个值:', lastValue);
    } else {
        console.log('数组为空,无值可获取');
    }
});

额外提示

如果需要在这个回调函数外部访问textArray,可以把数组声明在回调的外层作用域(比如全局变量或外层函数的变量),避免作用域限制导致无法调用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 13:40:58