小编典典

如何遍历以对象为成员的纯JavaScript对象?

javascript

如何遍历JavaScript对象中的所有成员,包括对象值。

例如,如何循环浏览(分别访问“ your_name”和“ your_message”)?

var validation_messages = {
    "key_1": {
        "your_name": "jimmy",
        "your_msg": "hello world"
    },
    "key_2": {
        "your_name": "billy",
        "your_msg": "foo equals bar"
    }
}

阅读 351

收藏
2020-04-25

共1个答案

小编典典

for (var key in validation_messages) {
    // skip loop if the property is from prototype
    if (!validation_messages.hasOwnProperty(key)) continue;

    var obj = validation_messages[key];
    for (var prop in obj) {
        // skip loop if the property is from prototype
        if (!obj.hasOwnProperty(prop)) continue;

        // your code
        alert(prop + " = " + obj[prop]);
    }
}
2020-04-25