Google Apps Script:如何从含空值的嵌套对象提取raw值
问题:处理Yahoo财经API返回数据中的空值与Null元素
我需要从Yahoo财经API返回的对象中提取所有type键下的raw值。现有代码在type键数据无缺失或空值时运行正常,但当部分type键包含null元素或为空时,会报错:
TypeError: Cannot read properties of null (reading 'reportedValue')
我希望修改代码,实现当type键没有raw值时,数组存入[[type, ''], ....],并移除所有null元素,请问应该如何修改原代码中的这一行:
var array = obj.timeseries.result.map(o => types.flatMap(type => o[type] ? [type, ...o[type].map(({ reportedValue: { raw } }) => raw)] : []));
完整原代码如下:
function test() { var url = 'https://query2.finance.yahoo.com/ws/fundamentals-timeseries/v1/finance/timeseries/CRWD?lang=en-US®ion=US&symbol=CRWD&padTimeSeries=true&type=annualTaxProvision,trailingTaxProvision,annualPretaxIncome,trailingPretaxIncome,annualInterestExpenseNonOperating,trailingInterestExpenseNonOperating,annualLongTermDebt,quarterlyLongTermDebt,annualCurrentDebt,quarterlyCurrentDebt,annualCashCashEquivalentsAndShortTermInvestments,quarterlyCashCashEquivalentsAndShortTermInvestments,annualFreeCashFlow,trailingFreeCashFlow&merge=false&period1=493590046&period2=1672980169&corsDomain=finance.yahoo.com' var obj = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getContentText(); obj = JSON.parse(obj); //Convert strings to object var types = obj.timeseries.result.flatMap(({ meta: { type } }) => type); var array = obj.timeseries.result.map(o => types.flatMap(type => o[type] ? [type, ...o[type].map(({ reportedValue: { raw } }) => raw)] : [])); array = Object.keys(array).map(k => [...array[k]]); // Convert object to array console.log(array) }
修改后的代码行及完整代码
将原代码中的数组生成行替换为以下代码,同时保留原有逻辑框架:
var array = obj.timeseries.result.map(o => types.flatMap(type => { // 处理type键不存在或为空数组的情况 if (!o[type] || o[type].length === 0) { return [type, '']; } // 过滤null元素并安全提取raw值,无值则返回空字符串 const rawValues = o[type] .filter(item => item !== null) // 移除null元素 .map(item => item?.reportedValue?.raw || ''); // 避免空对象报错,空值返回'' return [type, ...rawValues]; }) );
完整修改后的代码:
function test() { var url = 'https://query2.finance.yahoo.com/ws/fundamentals-timeseries/v1/finance/timeseries/CRWD?lang=en-US®ion=US&symbol=CRWD&padTimeSeries=true&type=annualTaxProvision,trailingTaxProvision,annualPretaxIncome,trailingPretaxIncome,annualInterestExpenseNonOperating,trailingInterestExpenseNonOperating,annualLongTermDebt,quarterlyLongTermDebt,annualCurrentDebt,quarterlyCurrentDebt,annualCashCashEquivalentsAndShortTermInvestments,quarterlyCashCashEquivalentsAndShortTermInvestments,annualFreeCashFlow,trailingFreeCashFlow&merge=false&period1=493590046&period2=1672980169&corsDomain=finance.yahoo.com' var obj = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getContentText(); obj = JSON.parse(obj); //Convert strings to object var types = obj.timeseries.result.flatMap(({ meta: { type } }) => type); var array = obj.timeseries.result.map(o => types.flatMap(type => { if (!o[type] || o[type].length === 0) { return [type, '']; } const rawValues = o[type] .filter(item => item !== null) .map(item => item?.reportedValue?.raw || ''); return [type, ...rawValues]; }) ); array = Object.keys(array).map(k => [...array[k]]); // Convert object to array console.log(array) }
修改说明
- 空值/缺失处理:判断
o[type]是否存在或为空数组,直接返回[type, ''],满足空值时存入空字符串的需求。 - Null元素过滤:通过
filter(item => item !== null)移除数组中的null元素,避免后续处理报错。 - 安全访问属性:使用可选链操作符
?.,避免直接访问null对象的reportedValue属性触发报错;当raw值不存在时,返回空字符串替代。
内容的提问来源于stack exchange,提问作者Newbie
相关产品推荐
相关产品推荐

