user2945241
user2945241

Reputation: 360

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

Upvotes: 0

Views: 164

Answers (1)

Neil Girardi
Neil Girardi

Reputation: 4923

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'

Upvotes: 1

Related Questions