对象长度不匹配

时间:2015-11-04 21:36:29

标签: javascript json

我以两种不同的方式输出一个对象的长度,它会产生两种不同的反应:

console.log(appData.HomeGateway.questions.length);
console.log(appData.HomeGateway.questions);

输出:

3
0: Object
1: Object
2: Object
3: Object
4: Object
5: Object
6: Object
7: Object
8: Object
length: 9

我知道为什么我会得到两种不同的长度?无论出于何种原因,appData.questions似乎都是从其他对象继承对象。

这是JSON:

var appData = {
    "HomeGateway": {
        "company": "",
        "title": {
            "english": "",
            "spanish": "",
            "russian": "",
            "otherlanguage": ""
        },
        "language": "english",
        "pressOKQuestion": {
            "left": {
                "english": "Press ",
                "spansih": "",
                "russian": ""
            },
            "right": {
                "english": " to move to the next question",
                "spanish": "\" to move to the next question",
                "russian": ""
            }
        },
        "pressOKMessage": {
            "left": {
                "english": "Press ",
                "spansih": "",
                "russian": ""
            },
            "right": {
                "english": " to move on",
                "spanish": "\" to move to the next question",
                "russian": ""
            }
        },
        "pressUpDown": {
            "left": {
                "english": "Please press ",
                "spanish": "",
                "russian": ""
            },
            "middle": {
                "english": " or ",
                "spanish": "",
                "russian": ""
            },
            "right": {
                "english": " on your remote control to select",
                "spanish": "",
                "russian": ""
            }
        },
        "questions": [{
            "url": "/question/1",
            "question": {
                "english": "Have you talked to your doctor today?",
                "spanish": "",
                "russian": ""
            },
            "view": "views/question.html",
            "controller": "loadData",
            "nextPage": "#/question/2",
            "answers": [{
                "id": 1,
                "answer": {
                    "english": "Yes",
                    "spanish": "Si",
                    "russian": ""
                }
            }, {
                "answer": {
                    "english": "No",
                    "spanish": "No",
                    "russian": ""
                },
                "message": "/message/3"
            }]
        }, {
            "url": "/question/2",
            "question": {
                "english": "On a scale of 0-10, how bad is your pain today?",
                "spanish": "",
                "russian": ""
            },
            "icon": "img/icons/1446585749_Doctor_Consultation.png",
            "iconSrc": "Broken Arm",
            "view": "views/question.html",
            "controller": "loadData",
            "nextPage": "#/question/3",
            "answers": [{
                "answer": {
                    "english": "Low",
                    "spanish": "",
                    "russian": ""
                },
                "category": "1-2"
            }, {
                "answer": {
                    "english": "Medium",
                    "spanish": "",
                    "russian": ""
                },
                "category": "3-4"
            }, {
                "answer": {
                    "english": "High",
                    "spanish": "",
                    "russian": ""
                },
                "category": "5-8",
                "message": "/message/4"
            }, {
                "answer": {
                    "english": "Very High",
                    "spanish": "",
                    "russian": ""
                },
                "category": "9-10",
                "message": "/message/4"
            }]

        }, {
            "url": "/question/3",
            "question": {
                "english": "Please enter your systolic blood pressure.",
                "spanish": "",
                "russian": ""
            },
            "icon": "img/icons/1446585795_Blood_Pressure_Kit.png",
            "iconSrc": "Blood Pressure",
            "view": "views/toggle-question.html",
            "controller": "loadData",
            "nextPage": "#/finished",
            "toggle": {
                "value": 100,
                "max": 500,
                "min": 50,
                "high": {
                    "value": 150,
                    "message": "/message/5"
                },
                "low": {
                    "value": 75,
                    "message": "/message/6"
                }
            }
        }],
        "messages": [{
            "url": "/message/1",
            "message": {
                "english": "Please talk to your nurse or doctor about why you do not want to.",
                "spanish": "",
                "russian": ""
            },
            "icon": "img/alert-red.png",
            "iconSrc": "Alert",
            "view": "views/message.html",
            "controller": "loadData",
            "nextPage": "#/message/2"
        }, {
            "url": "/message/2",
            "message": {
                "english": "Good job!",
                "spanish": "",
                "russian": ""
            },
            "icon": "img/icons/1446586860_Best_Choice.png",
            "iconSrc": "Thumbs Up!",
            "view": "views/message.html",
            "controller": "loadData",
            "nextPage": "#/question/1"
        }, {
            "url": "/home",
            "title": {
                "english": "Thank you for being part of today.",
                "spanish": "",
                "russian": ""
            },
            "subtitle": {
                "english": "Remember this device does not take place of a personal emergency response system or a call to 911.",
                "spanish": "",
                "russian": ""
            },
            "view": "views/home.html",
            "controller": "loadData",
            "nextPage": "#/question/1"
        }]
    }
}

注意:只是编辑它以反映我用来获得不同长度的方法。

2 个答案:

答案 0 :(得分:1)

没有# Melt to aggregate data df2 <- melt(df, id = c("id", "year", "month", "element")) for (i in 1:nrow(df2)) { df2$date[i] <- paste0(df2$year[i], df2$month[i], substr(df2$variable[i], 2,3)) } 。它必须是appData.questions,在两种情况下都给出三个(当然它确实如此)。以下是您自己数据的演示:

appData.HomeGateway.questions

答案 1 :(得分:1)

我认为你正在第二个console.log中跳过一个键

假设这不是实际的代码,而是为我们编写的,我假设您错过了第二个控制台中的问题键。它可能看起来像这样

import imp
import sys
from threading import Thread
from time import sleep

import wx

class MyFinder(object):
    def __init__(self):
        print('MyFinder initializing')
        if imp.lock_held():
            while imp.lock_held():
                print('import lock held')
                sleep(2)
            print('import lock released')
        else:
            print('import lock is not held')

    def find_module(self, module, package=None):
        print('MyFinder.find_module called with module name "{}", pakcage name "{}"'.format(module, package))
        return None


def test():
    sys.meta_path.append(MyFinder())
    from my_time import sleep
    count = 0
    while True:
        print("{} thread still running".format(count))
        count += 1
        sleep(2)


app = wx.App()
frame = wx.Frame(None, -1, 'simple.py')
frame.Show()
thread = Thread(target=test)
thread.setDaemon(True)
thread.start()
app.MainLoop()