0

I have a very large array of objects and some of the objects have a nameless property. What is an efficient way to remove the property?

[ 
  ...
  {
    "phone_number": "***-***-****",
    "category": "Abc",
    "link_to_company": "www.example.com",
    "email": "[email protected]",
    "company_name": "test company",
    "": "123", // this need to be removed
    "status": 1
  },
  ...
]
7
  • 3
    delete obj[''] Commented Mar 26, 2021 at 18:38
  • I'm not sure whether the critical issue is the empty value or empty key or both. Can you clarify, please? Commented Mar 26, 2021 at 18:40
  • issue is empty key. Commented Mar 26, 2021 at 18:46
  • Lawrence, thanks for the reply but what of there are unknown number of empty keys, I know I can use a loop to delete the empty keys but is there a better way to do that? Commented Mar 26, 2021 at 18:49
  • there can only be a single key of any name empty or not so can only be one. Commented Mar 26, 2021 at 18:55

1 Answer 1

2

A map function should do it.

array.map(v => { delete v[""]; return v })

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

2 Comments

thanks for the answer but what if there are multiple empty keys?
@ShafqatJamilKhan It's not possible that there are multiple empty keys in a single object-- all key names in an object are unique. Therefore there can be a maximum of one key per object that is an empty string. If you tried to set a second empty key, it would just replace the first.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.