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

时间:2021-05-30 13:38:27

I would like to put names (7, 8, 9) of each object from combinationsFormController into array.

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

The image comes from Firebug.

图像来自Firebug。

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

2 个解决方案

#1


1  

Something like this, 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)
      }
}

#2


1  

In modern browsers can use Object.keys() to create array

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

var arr = Object.keys(combinationsFormController);

For older browsers like IE<9 include the polyfill found in MDN Docs

对于像IE <9这样的旧浏览器,包括在MDN Docs中找到的polyfill

#1


1  

Something like this, 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)
      }
}

#2


1  

In modern browsers can use Object.keys() to create array

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

var arr = Object.keys(combinationsFormController);

For older browsers like IE<9 include the polyfill found in MDN Docs

对于像IE <9这样的旧浏览器,包括在MDN Docs中找到的polyfill