【问题标题】:Replace "null" string into null in Javascript在Javascript中将“null”字符串替换为null
【发布时间】:2021-10-30 23:29:12
【问题描述】:

我有一个对象数组,其中某些键的值将是和“null”作为字符串,我想将其转换为 null

我尝试如下代码

 let obj = [{
                        "fundcode": "DE",
                        "fundname": "Defens",
                        "fundunits": "4944.43463",
                        "fundvalue": "545508.594971714",
                        "nav": "110.3278",
                        "navdeclareddate": "2021-09-01 00:00:00.0"
                    },
                    {
                    "fundcode": "EQ",
                    "fundname": "Equit",
                    "fundunits": "null",
                    "fundvalue": "null",
                    "nav": "null",
                    "navdeclareddate": "null"
                }]
let newJson = Object.keys(obj).map(item =>  obj[item] === "null" ? null : obj[item])

我想要 newJson 如下所示

[{
                        "fundcode": "DE",
                        "fundname": "Defens",
                        "fundunits": "4944.43463",
                        "fundvalue": "545508.594971714",
                        "nav": "110.3278",
                        "navdeclareddate": "2021-09-01 00:00:00.0"
                    },
{
                    "fundcode": "EQ",
                    "fundname": "Equit",
                    "fundunits": null,
                    "fundvalue": null,
                    "nav": null,
                    "navdeclareddate": null
                }]

【问题讨论】:

  • 你当前的代码有什么问题?

标签: javascript node.js arrays json multidimensional-array


【解决方案1】:

如果从 json 解析,请尝试 JSON.parse() 中的 reviver 函数

let json = `[{
  "fundcode": "EQML",
  "fundname": "Equity Managed Fund",
  "fundunits": "null",
  "fundvalue": "null",
  "nav": "null",
  "navdeclareddate": "null"
}]`

let obj = JSON.parse(json, (k, v) => v === 'null' ? null : v)

console.log(obj)

【讨论】:

  • 注意:问题用 JSON 标记
  • 有趣的方法。不知道 JSON 中的 replacer。我认为这是迄今为止最优雅的解决方案。但为了对原始代码公平起见,它应该在一个对象上完成,而不是一个字符串;)
  • @vanowm 对不起,函数被称为reviver
【解决方案2】:

Object.entriesObject.fromEntries可以在这里使用

let obj = [{
    "fundcode": "DE",
    "fundname": "Defens",
    "fundunits": "4944.43463",
    "fundvalue": "545508.594971714",
    "nav": "110.3278",
    "navdeclareddate": "2021-09-01 00:00:00.0"
  },
  {
    "fundcode": "EQ",
    "fundname": "Equit",
    "fundunits": "null",
    "fundvalue": "null",
    "nav": "null",
    "navdeclareddate": "null"
  }
]

let result = obj.map(
  o => Object.fromEntries(Object.entries(o).map(
    ([key, value]) => value == "null" ? [key, null] : [key, value]))
)
console.log(result);

【讨论】:

    【解决方案3】:

    你几乎做到了,只是缺少一点细节,obj 是一个数组,而不是一个对象

    let obj = [{
                        "fundcode": "EQML",
                        "fundname": "Equity Managed Fund",
                        "fundunits": "null",
                        "fundvalue": "null",
                        "nav": "null",
                        "navdeclareddate": "null"
                    }]
    let newJson = obj.map(o => (Object.keys(o).forEach(item => o[item] = o[item] == "null" ? null : o[item]),o))
    
    console.log(newJson)

    【讨论】:

      【解决方案4】:

      您可以使用Object.keysforEachmap 实现此结果。

      let obj = [{
          fundcode: "DE",
          fundname: "Defens",
          fundunits: "4944.43463",
          fundvalue: "545508.594971714",
          nav: "110.3278",
          navdeclareddate: "2021-09-01 00:00:00.0",
        },
        {
          fundcode: "EQ",
          fundname: "Equit",
          fundunits: "null",
          fundvalue: "null",
          nav: "null",
          navdeclareddate: "null",
        },
      ];
      
      const newJson = obj.map((o) => {
        const newObj = {};
        Object.keys(o).forEach((k) => (newObj[k] = o[k] === "null" ? null : o[k]));
        return newObj;
      });
      
      console.log(newJson);
      /* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
      
      .as-console-wrapper {
        max-height: 100% !important;
        top: 0;
      }

      【讨论】:

      • 如果您已经拥有o,为什么还需要newObj
      猜你喜欢
      • 2021-04-04
      • 1970-01-01
      • 2020-02-09
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 2012-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多