Form of an array of property names found in a JavaScript object

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 these property keys separately, as after

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

Number of dynamic properties

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

0
source share
2 answers

Object.keys()

var columns = {ContributionType: "Employer Contribution",
               Employee1: "0",
               Employee2: "0",
               Employee3: "0"
              };
var keys = Object.keys(columns);
console.log(keys);
Run codeHide result
+1
source
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);
}
}
+1
source

Source: https://habr.com/ru/post/1668122/


All Articles