How to access each member in JavaScript

Asked 2 years ago, Updated 2 years ago, 62 views

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

For example, I'd like to see a member of an object. How do you create a repeating sentence that searches for your_name: Jimmy for each key?

javascript object

2022-09-21 14:53

1 Answers

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]);
    }
}

Do it like this.


2022-09-21 14:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.