在数组中插入对象失败

时间:2016-05-10 23:54:10

标签: javascript

我从mongodb获取此数据

{
    "_id" : "RLvWTcsrbRXJeTqdB",
    "examschoolid" : "5FF2JRddZdtTHuwkx",
    "examsubjects" : [
        {
            "subject" : "Z4eLrwGwqG4pw4HKX"
        },
        {
            "subject" : "fFcWby8ArpboizcT9"
        }
    ],
    "examay" : "NrsP4srFGfkc5cJkz",
    "examterm" : "5A5dNTgAkdRr5j53j",
    "examclass" : "gYF2wE4wBCRy9a3ZC",
    "examname" : "First",
    "examdate" : ISODate("2016-05-07T22:41:00Z"),
    "examresultsstatus" : "notreleased"
}

我想迭代examsubjects并最终在我的数组中插入一个对象并得到一个这样的数组

"examsubjects" : [
{
"Z4eLrwGwqG4pw4HKX" : "0",
"fFcWby8ArpboizcT9" : "0"
}
],

这是我的代码

 var result = new Array();
    for (i = 0; i < doc.examsubjects.length; i++) {
        var arr = {};
        for (var prop in doc.examsubjects[i]) {
            arr[doc.examsubjects[i][prop]] = 0;

        }
        result.push(arr);
    }

给了我这个数组

"examsubjects" : [
        {
            "Z4eLrwGwqG4pw4HKX" : 0
        },
        {
            "fFcWby8ArpboizcT9" : 0
        }
    ],

我如何获得我想要的阵列?。

1 个答案:

答案 0 :(得分:0)

在您的尝试中,初始化一个空数组并为browsePath = "/Users/myUserName/Documents/someFolder/" MyBrowseTypes = """xls"", ""doc""" browseMulti = True If browseMulti Then myPrompt = "Please select a file or files" Else myPrompt = "Please select a file" End If myScript = "set theFiles to (choose file of type {" & MyBrowseTypes & "} " & _ "with prompt """ & myPrompt & """ default location (""" & _ browsePath & """ as posix file as alias) multiple selections allowed " & browseMulti & ")" & vbNewLine & _ "set {TID, text item delimiters} to {text item delimiters, "",""}" & vbNewLine & _ "set theFiles to theFiles as text" & vbNewLine & _ "set text item delimiters to TID" & vbNewLine & _ "return theFiles" MyFiles = MacScript(myScript) Debug.Print MyFiles 数组中的每个对象推送一个新对象,当您想要做的是将所有考试科目中的数据放入一个对象时,当你然后显然想要在阵列中。实现这一目标的众多方法之一是:

examsubjects