表单在JavaScript对象中找到的属性名称数组

时间:2015-07-21 14:57:38

标签: javascript lodash

我有以下对象

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

从这里我需要单独使用属性键来形成一个数组,如下面的

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

属性数量是动态的

问题: 如何使用lodash或纯JavaScript实现此目的?

2 个答案:

答案 0 :(得分:1)

Object.keys()

var columns = {ContributionType: "Employer Contribution",
               Employee1: "0",
               Employee2: "0",
               Employee3: "0"
              };
var keys = Object.keys(columns);
console.log(keys);

答案 1 :(得分: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);
}
}