surya
surya

Reputation: 191

How to remove the null values from the json input

how to remove the property that contains null value from the below input

var data = [
        { "Id": "parent", "Function": "Project Management", "Phase": "(Null)" },
        { "Id": "1", "Function": "R&D Team", "Phase": "parent" },
        { "Id": "2", "Function": "HR Team", "Phase": "parent" },
        { "Id": "3", "Function": "Sales Team", "Phase": "parent" },
        { "Id": "4", "Function": "Philosophy", "Phase": "1" },
        { "Id": "5", "Function": " Organization", "Phase": "1" },

     ];

Upvotes: 1

Views: 162

Answers (1)

Sergii Rudenko
Sergii Rudenko

Reputation: 2684

You can do it like this:

data = data.map(obj => Object.keys(obj).reduce((prev, prop) => {
  // you can check for '(Null)', null or for any different kind of value here
  if (obj[prop] != '(Null)') {
    prev[prop] = obj[prop]
  }
  return prev
}, {}))

Upvotes: 1

Related Questions