rahul5140
rahul5140

Reputation: 170

Replace "null" string into null in Javascript

I have an array of objects in that for some keys value will be and "null" as a string I want to be convert it into null

I was trying as below code

 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])

I want newJson as like below

[{
                        "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
                }]

Upvotes: 0

Views: 198

Answers (4)

User863
User863

Reputation: 20039

In case of parsing from json, try reviver function in JSON.parse()

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)

Upvotes: 4

vanowm
vanowm

Reputation: 10201

You almost did it, just little detail is missing, the obj is an array, not an object

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)

Upvotes: 1

DecPK
DecPK

Reputation: 25408

You can achieve this result using Object.keys, forEach and map.

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;
}

Upvotes: 0

Jamiec
Jamiec

Reputation: 136104

Object.entries and Object.fromEntries can be used here

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);

Upvotes: 2

Related Questions