我有以下对象
var columns = {ContributionType: "Employer Contribution",
Employee1: "0",
Employee2: "0",
Employee3: "0"
};
从这里我需要单独使用属性键来形成一个数组,如下面的
var keys=["ContributionType", "Employee1", "Employee2", "Employee3"];
属性数量是动态的
问题: 如何使用lodash或纯JavaScript实现此目的?
答案 0 :(得分:1)
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);
}
}