怎么改变数组中字段的格式

原数组

[
  {
    "name":111,
    "data": [
      {
        "code": "vivo20220218IM",
        "itemName": "vivo2018-2019赛季NBA整合营销项目Pitch",
        "year": 2021,
        "conamount": [
          {
            "month": 1,
            "amount": 111
          },
          {
            "month": 2,
            "amount": 222
          }
        ],
        "targetGi": "487898.00",
        "realOutpay": "488.00"
      }
    ],
    "customerTotal": {
      "conamount": [
        {
          "month": 1,
          "amount": 0
        },
        {
          "month": 2,
          "amount": 0
        }
      ],
      "targetGi": "487898.00",
      "realOutpay": "488.00"
    }
  },
  {
    "name":222,
    "data": [
      {
        "code": "vivo20220218IM",
        "itemName": "vivo2018-2019赛季NBA整合营销项目Pitch",
        "year": 2022,
        "conamount": [
          {
            "month": 1,
            "amount": 111
          },
          {
            "month": 2,
            "amount": 222
          }
        ],
        "targetGi": "487898.00",
        "realOutpay": "488.00"
      }
    ],
    "customerTotal": {
      "conamount": [
        {
          "month": 1,
          "amount": 0
        },
        {
          "month": 2,
          "amount": 0
        }
      ],
      "targetGi": "487898.00",
      "realOutpay": "488.00"
    }
  }
]

通过function把想要的字段改成千分符字符串

/**换算千分钱格式 */
    formatNum(num) {
      let nub = Number(num);
      let str = nub + '',
      intSum = str.substring(0,str.indexOf(".")).replace( /\B(?=(?:\d{3})+$)/g, ',' ),//取到整数部分
      dot = str.substring(str.length,str.indexOf(".")),//取到小数部分
      ret = intSum + dot; //拼到一起
      return ret;  
    },

改后

[
  {
    "name":111,
    "data": [
      {
        "code": "vivo20220218IM",
        "itemName": "vivo2018-2019赛季NBA整合营销项目Pitch",
        "year": 2021,
        "conamount": [
          {
            "month": 1,
            "amount": "111"
          },
          {
            "month": 2,
            "amount": "222"
          }
        ],
        "targetGi": "487,898.00",
        "realOutpay": "488.00"
      }
    ],
    "customerTotal": {
      "conamount": [
        {
          "month": 1,
          "amount": "0"
        },
        {
          "month": 2,
          "amount": "0"
        }
      ],
      "targetGi": "487,898.00",
      "realOutpay": "488.00"
    }
  },
  {
    "name":222,
    "data": [
      {
        "code": "vivo20220218IM",
        "itemName": "vivo2018-2019赛季NBA整合营销项目Pitch",
        "year": 2022,
        "conamount": [
          {
            "month": 1,
            "amount": "111"
          },
          {
            "month": 2,
            "amount": "222"
          }
        ],
        "targetGi": "48,7898.00",
        "realOutpay": "488.00"
      }
    ],
    "customerTotal": {
      "conamount": [
        {
          "month": 1,
          "amount": "0"
        },
        {
          "month": 2,
          "amount": "0"
        }
      ],
      "targetGi": "48,7898.00",
      "realOutpay": "488.00"
    }
  }
]
阅读 1.2k
2 个回答

假如上面的数据保存在变量 list 中,那么就是一个循环,再嵌套一个循环就可以解决的啊

for (const { data, customerTotal } of list) {
    data.forEach(it => {
        it.targetGi = formatNum(it.targetGi);
        it.realOutpay = formatNum(it.realOutpay);
    });

    customerTotal.targetGi = formatNum(customerTotal.targetGi);
    customerTotal.realOutpay = formatNum(customerTotal.realOutpay);
}

不过那个格式化数字的函数好像有问题,格式不出来,可以用下面这一个

function formatNum(num) {
    return Number(num).toLocaleString(undefined, {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2
    });
}

由于两个对象结构差不多,处理语句是一样的,所以可以提取函数

function formatObjNums(it) {
    it.targetGi = formatNum(it.targetGi);
    it.realOutpay = formatNum(it.realOutpay);
}

for (const { data, customerTotal } of list) {
    data.forEach(formatObjNums);
    formatObjNums(customerTotal);
}

仅仅是 targetGi 字段 ?

var list = [
  {
    'name': 111,
    'data': [
      {
        'code': 'vivo20220218IM',
        'itemName': 'vivo2018-2019赛季NBA整合营销项目Pitch',
        'year': 2021,
        'conamount': [
          {
            'month': 1,
            'amount': 111,
          },
          {
            'month': 2,
            'amount': 222,
          },
        ],
        'targetGi': '487898.00',
        'realOutpay': '488.00',
      },
    ],
    'customerTotal': {
      'conamount': [
        {
          'month': 1,
          'amount': 0,
        },
        {
          'month': 2,
          'amount': 0,
        },
      ],
      'targetGi': '487898.00',
      'realOutpay': '488.00',
    },
  },
  {
    'name': 222,
    'data': [
      {
        'code': 'vivo20220218IM',
        'itemName': 'vivo2018-2019赛季NBA整合营销项目Pitch',
        'year': 2022,
        'conamount': [
          {
            'month': 1,
            'amount': 111,
          },
          {
            'month': 2,
            'amount': 222,
          },
        ],
        'targetGi': '487898.00',
        'realOutpay': '488.00',
      },
    ],
    'customerTotal': {
      'conamount': [
        {
          'month': 1,
          'amount': 0,
        },
        {
          'month': 2,
          'amount': 0,
        },
      ],
      'targetGi': '487898.00',
      'realOutpay': '488222.00',
    },
  },
]

function numberFormat (number) {
  return Number(number).toLocaleString('en-US', { minimumFractionDigits: 2 })
}

function transform (data) {
  return {
    targetGi: numberFormat(data.targetGi),
    realOutpay: numberFormat(data.realOutpay),
  }
}

const list2 = list.map(item => {
  const data = item.data.map(data => {
    const transformed = transform(data)
    return Object.assign({}, data, transformed)
  })

  const customerTotal = Object.assign({}, item.customerTotal, {
    realOutpay: numberFormat(item.customerTotal.realOutpay),
    targetGi: numberFormat(item.customerTotal.targetGi),
  })

  // or
  // const customerTotal = Object.assign({}, item.customerTotal, transform(item.customerTotal))

  return Object.assign({}, item, { data, customerTotal })
})

console.log(list2)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题