0

I have the following object

var columns = {ContributionType: "Employer Contribution",
               Employee1: "0",
               Employee2: "0",
               Employee3: "0"
              };

From this I need to form an array with they property keys alone like following

var keys=["ContributionType", "Employee1", "Employee2", "Employee3"];

The number of properties is dynamic

Question: How can I achieve this using lodash or pure JavaScript?

RandomUser
  • 1,843
  • 8
  • 33
  • 65

2 Answers2

1

Object.keys()

var columns = {ContributionType: "Employer Contribution",
               Employee1: "0",
               Employee2: "0",
               Employee3: "0"
              };
var keys = Object.keys(columns);
console.log(keys);
epascarello
  • 204,599
  • 20
  • 195
  • 236
1
var arr=[];
for (var key in columns)
{
//by using hasOwnProperty(key) we make sure that keys of
//the prototype are not included if any
if(columns.hasOwnProperty(key))
{
    arr.push(key);
}
}
Untimely Answers
  • 512
  • 1
  • 6
  • 18