2

I have a json array of objects and want to find a way to get the property names into another array.

[
 {
    "ID": 12345,
    "SID": 1111,
    "DataPoint1": [
                    {
                      "Name": "SD",
                      "Activity": "KT",
                      "Group":"Test"
                    }
  }
]

I want to be able to extract all the property names of DataPoint1 into its own array:

New-->

[Name, Activity, Group]

I was looking into loadash but couldn't find anything. Any ideas? Thanks.

2 Answers 2

1

You could use _.keys(data[0].DataPoint1[0]) to get the keys as an array.

const data = [{
  "ID": 12345,
  "SID": 1111,
  "DataPoint1": [{
    "Name": "SD",
    "Activity": "KT",
    "Group": "Test"
  }]
}]

console.log(_.keys(data[0].DataPoint1[0]))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

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

1 Comment

Thanks Jonathan! Marked as answered :)
0

Answered my own question :) This did the trick:

Object.getOwnPropertyNames(array[0].DataPoint1[0])

Comments

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.