获取对象的属性名称 [英] Getting the object's property name
本文介绍了获取对象的属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道JavaScript中是否有任何方法可以循环遍历这样的对象。
I was wondering if there was any way in JavaScript to loop through an object like so.
for(var i in myObject) {
// ...
}
但得到名字这样的每个属性。
But get the name of each property like this.
for(var i in myObject) {
separateObj[myObject[i].name] = myObject[i];
}
我似乎无法在Google上找到类似的内容。他们说用它们传递变量的名称,但这不是我想要实现的选项。
I can't seem to find anything like it on Google. They say to pass the names of the variables with them but this is not an option for what I am trying to achieve.
感谢您提供任何帮助。
推荐答案
var myObject = { a: 'c', b: 'a', c: 'b' };
var keyNames = Object.keys(myObject);
console.log(keyNames); // Outputs ["a","b","c"]
Object.keys()
为您提供属于输入对象的属性名称数组。
Object.keys()
gives you an array of property names belonging to the input object.
这篇关于获取对象的属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文