Return JSON 属性

Return JSON properties

我正在尝试 return 我的 JSON 对象的属性。我的 JSON 文件对象如下所示:

{ Products: 
   { 
     'Cars': { tableFields: [Object] },
     'Planes': { tableFields: [Object] } 
   } 
}

我正在尝试 return 一个包含 Products' 属性的数组 - CarsPlanes。例如 - 我希望最终结果是以下数组:

['Cars', 'Planes']

有人可以帮忙吗?

谢谢!

您可以使用Object.keys()函数:

var data = {
    Products: {
        'Cars': {
            tableFields: [ Object ]
        },
        'Planes': {
            tableFields: [ Object ]
        }
    }
};

var result = Object.keys(data.Products);
console.log(result);
var keys = [];
for ( var key in Products )
{
    //We only want the direct properties
    if ( Products.hasOwnProperty( key ) ) keys[ keys.length ] = key;
}