0

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
                }]
1
  • What is wrong with your current code? Commented Sep 1, 2021 at 13:00

4 Answers 4

4

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)

Sign up to request clarification or add additional context in comments.

3 Comments

Note: The question is tagged with JSON
Interesting method. Didn't know about the replacer in JSON. I think this is the most elegant solution so far. But just to be fair to the original code, it should be done on an object, not a string ;)
@vanowm Sorry that function is called as reviver
2

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

Comments

1

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)

Comments

0

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

1 Comment

why do you need a newObj if you already have one as o ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.