0
  {
        "Gender": "male",
        "SiteId": -99,
        "LastName": "Bobby",
        "BirthDate": "2000-01-15T06:00:00.000Z",
        "FirstName": "Ricky",
        "HomeLocation": {},
        "ClientCreditCard": {},
        "CustomClientFields": {}
    }

I've tried a few options I found here, but can't seem to understand what I'm doing wrong. I've tried:

let o = Object.keys(obj)
  .filter((k) => obj[k] != null)
  .reduce((a, k) => ({ ...a, [k]: obj[k] }), {});

and

var output = JSON.parse(JSON.stringify(obj));

Also, any recommended courses that focus on data manipulation for arrays/objects would be appreciated!

phuzi
  • 12,078
  • 3
  • 26
  • 50
  • Are there examples where those objects might hold properties or will they always be empty? – Andy Jun 14 '22 at 22:28
  • Your code seems to work fine for filtering out `null` (first snippet) and `undefined` (first and second snippet) values. All you need is to add a [test for empty objects](https://stackoverflow.com/q/679915/1048572) as well. – Bergi Jun 14 '22 at 23:01
  • @Andy Yes. If they have properties then I need them to pass through. – Luke McFadden Jun 15 '22 at 02:49

1 Answers1

0

The function you are looking for is Object.entries()

This will give you an array that looks like this: [[key, value], [key, value], [key, value]]

From there you can use that filter function to check if the value matches your condition.

Object.entries(yourObject).filter(item => item[1] != null)

Note: I don't think that checking only for != null will be enough based on the sample you gave.