简体   繁体   中英

How to access array from JSON object in javascript?

Hello i wanted to ask how to access array in javascript from JSON object like this:

Object {results: "success", message: "Your message have been sent successfully",           error_id_array: Array[1]}
  error_id_array: Array[1]
    0: "2"
    length: 1
    __proto__: Array[0]
  message: "Your message have been sent successfully"
  results: "success"
  __proto__: Object

This question is related to this How to work with array returned from PHP script using AJAX? question. Thank you

As Moo-Juice said, that is not a valid object. Here is an example of a valid object and how you would access its properties:

var myObj = {
    myString : 'Hello world!',
    myArray : ['red', 'yellow', 'blue'],
    alertString : function() {
        var message = this.myString;
        for (var i = 0; i < this.myArray.length; i++) {
            message += ' ' + this.myArray[i];
        }
        alert(message);
    }

};

myObj.alertString();

// triggers a modal that says 'Hello world! red yellow blue'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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