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

如何从指定JSON数组中提取validade字段值并仅获取其年份部分用于赋值?

Solution to Extract Year from validade Field

Got it, let's sort this out for you! Since your validade field follows a consistent YYYY-MM-DD format, there are a few simple ways to pull just the year part and assign it to digiAno:

Option 1: Split the String (Most Readable for Fixed Format)

Split the date string by hyphens and grab the first element of the resulting array—this works perfectly because the year is always the first segment:

cartao.validade.split('-')[0]

Option 2: Substring the First 4 Characters

Directly extract the first 4 characters of the string, which correspond exactly to the year:

cartao.validade.substring(0, 4)

Option 3: Use Date Object (For Flexible Date Handling)

If you might need to adapt to different date formats later, converting to a Date object and fetching the full year is a robust choice (your ISO format plays nicely with this method):

new Date(cartao.validade).getFullYear()

Updated Code

Here’s your modified code with the digiAno field using the split method (swap in any of the options above if you prefer):

this.dadosCC.reset({ 
  'parcelas': '1', 
  'digiNumero': ['**** **** ****', cartao.mask], 
  'digiNome': cartao.holder, 
  'digiMes': '', 
  'digiAno': cartao.validade.split('-')[0], // Updated line
  'valor': this.cartS.cart.valorTotal, 
  'cpfInput': this.userS.user.cpf, 
  'cardToken': this.cardToken, 
  'digiCVV':'' 
});

All three methods will correctly pull 2026 from your sample validade value of "2026-06-01".

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 11:37:44