javascript - 如何删除对象的键和值之间的空格

标签 javascript php

我想在使用 json_decode 方法解码之前删除键或对象名称之间的所有空格。

Income cut 我可以做到 Income_cut 吗?

有没有办法解决这个问题。

{
    "Housing": 0,
    "Late Comers": 0,
    "Income cut": "12500",
    "Study Allowance": 0,
    "test": 0,
    "i": 0,
    "staff_no": "9",
    "staff_name": "Abeja Vicky",
    "staff_department": "Production Staff",
    "staff_position": "Production Manager",
    "staff_salary": "0",
    "GrossValue": 12500,
    "GrossSalary": 0,
    "NSSF": 0,
    "PAYE": 0,
    "GrossValueAddiotion": 0,
    "GrossValueDecuction": 12500,
    "netPay": -12500,
    "Balance": 0,
    "Paidx": 0,
    "balance": -12500
}

最佳答案

假设没有嵌套简单的 json 对象,下面将在发送到服务器之前工作

var input ={
    "Housing": 0,
    "Late Comers": 0,
    "Income cut": "12500",
    "Study Allowance": 0,
    "test": 0,
    "i": 0,
    "staff_no": "9",
    "staff_name": "Abeja Vicky",
    "staff_department": "Production Staff",
    "staff_position": "Production Manager",
    "staff_salary": "0",
    "GrossValue": 12500,
    "GrossSalary": 0,
    "NSSF": 0,
    "PAYE": 0,
    "GrossValueAddiotion": 0,
    "GrossValueDecuction": 12500,
    "netPay": -12500,
    "Balance": 0,
    "Paidx": 0,
    "balance": -12500
}

var output = {};
for(var key in input){
  output[key.replace(/\s+/,'_')] = input[key];
}

console.log(output);

关于javascript - 如何删除对象的键和值之间的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35541239/

相关文章:

javascript - 防止 redux 存储值在组件中再次被使用

javascript - NativeScript - 地理位置 : right way to use getCurrentLocation promise function

javascript - 在维护 instanceof 的 Javascript 中克隆一个对象

php - 在不支持它的 php 5.2 上使用 CRYPT_BLOWFISH

javascript - 这会导致性能泄漏还是其他原因?

javascript - 从另一个站点获取 HTML 内容

php - 如何在 php 数组中搜索 mysql 表中的所有项目?

php - python到php代码转换

PHP - 生产环境的明智/优雅/优雅的错误处理

php - 如何在 javascript 中验证 MM/YY 的日期?