简体   繁体   English

获取JavaScript对象的键

[英]Getting the keys of a JavaScript object

I have made an object like: 我做了一个像这样的对象:

var courtdocument=    {
    'CFADocuments': {
        cv: [
            "CFA_Pack_Cover_Letter.docx",
            "Countersigned-CFA-Terms-and-Conditions-Letter.docx",
            "Test-cfa-documents - Copy - Copy.docx"
        ]
    },
    'LetterOfClaim': {
        cv: [
             "CFA_Pack_Cover_Letter.docx",
            "Countersigned-CFA-Terms-and-Conditions-Letter.docx"
        ]
    },
    'LetterOfInstruction': {
        cv: [
             "CFA_Pack_Cover_Letter.docx",
            "Countersigned-CFA-Terms-and-Conditions-Letter.docx"

        ]
    },
    Letters: {
        cv: [

        ]
    },
    'MedicalRecords': {
        cv: [

        ]
    },
    'medicalreports': {
        cv: [

        ]
    }
}

How will I get this set? 我将如何获得此套装?

CFADocuments
LetterOfClaim
LetterOfInstruction
Letters
MedicalRecords
medicalreports

If you are looking for the different keys in the courtdocument obejct then in modern browsers you can use Object.keys() - IE < 9 not supported you can use a shim as shown in the mdn docs 如果要在courtdocument文档对象中查找不同的密钥,则在现代浏览器中,您可以使用Object.keys() -IE <9不支持,您可以使用垫片,如mdn docs中所示

console.log(Object.keys(courtdocument))

Demo: Fiddle 演示: 小提琴

您只需要使用courtdocument.CFADocumentscourtdocument["CFADocuments"]

If you want to get the set of values, try: 如果要获取一组值,请尝试:

var result = []

for (var prop in courtdocument){
    if (courtdocument.hasOwnProperty(prop){
        result.push(courtdocument[prop])
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM