1

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" },

     ];
5
  • Unclear: What exactly need to remove? Property/Object? Showing no efforts. Commented Jan 5, 2018 at 6:09
  • need to remove the property that contains null value Commented Jan 5, 2018 at 6:12
  • edit the question and add the code you've tried to solve this problem. Commented Jan 5, 2018 at 6:13
  • What if { "Id": "parent", "Function": "Project Management", "Phase": "" } or { "Id": "parent", "Function": "Project Management", "Phase": null}? Commented Jan 5, 2018 at 6:18
  • @Mamun { "Id": "parent", "Function": "Project Management", "Phase": "" } or { "Id": "parent", "Function": "Project Management", "Phase": null} For this case i have no issues Commented Jan 5, 2018 at 6:24

1 Answer 1

1

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
}, {}))
Sign up to request clarification or add additional context in comments.

1 Comment

Also You can do as : data.forEach((d, v)=> { for(x in d) { if(d[x] == "(Null)") { delete d[x] }; }});

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.