1. 程式人生 > 實用技巧 >快速將英文展示改為中文展示

快速將英文展示改為中文展示

原來的資料,獲取後為展示英文的文字

// 屬性標籤
const attributeData = [
  {
    tagType: 'color',
    info: [
      'black',
      'white',
      'blue',
      'gray',
      'brown',
      'red',
      'green',
      'pink',
      'orange',
      'yellow',
      'purple',
    ],
  },
  {
    tagType: 'age',
    info: ['young', 'adult', 'old'],
  },
  {
    tagType: 
'gender', info: ['male', 'female'], }, ];

要改成中文的,但是不能直接把他們的value改為中文,因為有些邏輯可能就是通過value進行判斷的,要的只是把展示文字變成中文。

我們新建立一個和原資料一樣的資料,value為中文的:

// 屬性標籤-中文
const attributeData_ch = [
  {
    tagType: '顏色',
    info: [
      '黑色',
      '白色',
      '藍色',
      '灰色',
      '棕色',
      '紅色',
      '綠色',
      
'粉色', '橙色', '黃色', '紫色', ], }, { tagType: '姿態', info: [ '坐著', '站著', '走著', '跑著', '跳著', '躺著(臉朝上)', '彎腰著', '趴著(臉朝下)', '蹲著', ], }, { tagType: '年齡', info: ['未成年', '成年', '老的'], }, ];

即可拿到

attributeData.forEach((item,i) => {
  const item_ch
=attributeData_ch[i]; console.log(item.tagType,item_ch.tagType) item.info.forEach((info,j) => { const info_ch = item_ch.info[j] console.log(info,info_ch) }) })

結果:

在對應的展示位使用中文即可。