复杂数据(对象数组)里的字段替换

实现效果:
将下图所示英文转换成中文(后端返回数据有对应字段,具体见代码段)
在这里插入图片描述
后端返回数据代码段:

//数据结构如下
const billListData = {
    pay: [
      {
        type: 'foods',
        name: '餐饮',
        list: [
          { type: 'food', name: '餐费' },
          { type: 'drinks', name: '酒水饮料' },
          { type: 'dessert', name: '甜品零食' },
        ],
      },
      {
        type: 'taxi',
        name: '出行交通',
        list: [
          { type: 'taxi', name: '打车租车' },
          { type: 'longdistance', name: '旅行票费' },
        ],
      },
      {
        type: 'recreation',
        name: '休闲娱乐',
        list: [
          { type: 'bodybuilding', name: '运动健身' },
          { type: 'game', name: '休闲玩乐' },
          { type: 'audio', name: '媒体影音' },
          { type: 'travel', name: '旅游度假' },
        ],
      },
      {
        type: 'daily',
        name: '日常支出',
        list: [
          { type: 'clothes', name: '衣服裤子' },
          { type: 'bag', name: '鞋帽包包' },
          { type: 'book', name: '知识学习' },
          { type: 'promote', name: '能力提升' },
          { type: 'home', name: '家装布置' },
        ],
      },
      {
        type: 'other',
        name: '其他支出',
        list: [{ type: 'community', name: '社区缴费' }],
      },
    ],
    income: [
      {
        type: 'professional',
        name: '其他支出',
        list: [
          { type: 'salary', name: '工资' },
          { type: 'overtimepay', name: '加班' },
          { type: 'bonus', name: '奖金' },
        ],
      },
      {
        type: 'other',
        name: '其他收入',
        list: [
          { type: 'financial', name: '理财收入' },
          { type: 'cashgift', name: '礼金收入' },
        ],
      },
    ],
  }

实现代码段:

const billTypeToName = Object.keys(billListData).reduce((prev, key) => {
    billListData[key].forEach(bill => {
      bill.list.forEach(item => {
        prev[item.type] = item.name
      })
    })
    return prev
  }, {})

使用billTypeToName函数:
<div className="billType">{billTypeToName[item.useFor]}</div>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值