如何获取对象的名称,然后将它们放入数组中

时间:2015-02-26 15:51:27

标签: javascript jquery

我想将combinationsFormController中每个对象的名称(7,8,9)放入数组中。

图片来自Firebug。

enter image description here

2 个答案:

答案 0 :(得分:1)

像这样,example here

var combinationsFormController  = {
    prop1: {'prop1':7},
    prop2: {'prop2':8},
    prop3: {'prop2':9},
    1: {'prop1':7},
    2: {'prop2':8},
    3: {'prop2':9},
    7: {'prop1':7},
    8: {'prop2':8},
    9: {'prop2':9},
};

var arrProps=[];
for ( property in combinationsFormController  ) {
      if (combinationsFormController.hasOwnProperty(property))
      {
         console.log( property ); 
         arrProps.push(property)
      }
}

答案 1 :(得分:1)

在现代浏览器中,可以使用Object.keys()创建数组

var arr = Object.keys(combinationsFormController);

对于像IE< 9这样的旧浏览器,包括polyfill found in MDN Docs