al_kasih_empatix
al_kasih_empatix

Reputation: 127

multidimensional array with JavaScript just like php

How do I write multidimensional array with JavaScript just like php in where I can loop it and search it based on the key value.

        var domainme = array( 
                [0] array (
                        "domain" => "one.com",
                        "database" => "one",
                        "api" => "one.php"
                    )               
                [1] array (
                        "domain" => "two.com",
                        "database" => "two",
                        "api" => "two.php"
                    )               
                [2] array (
                        "domain" => "three.com",
                        "database" => "three",
                        "api" => "three.php"
                    )               

Upvotes: 1

Views: 48

Answers (2)

R3tep
R3tep

Reputation: 12864

You need to use an array of object.

Like :

var domainme = [{
  "domain": "one.com",
  "database": "one",
  "api": "one.php"
}, {
  "domain": "two.com",
  "database": "two",
  "api": "two.php"
}, {
  "domain": "three.com",
  "database": "three",
  "api": "three.php"
}]

To get a value you can do :

domainme[0]["domain"] // get : 'one.com'
//or 
domainme[0].domain // get : 'one.com'

Upvotes: 3

T.J. Crowder
T.J. Crowder

Reputation: 1075149

JavaScript distinguishes between arrays and objects (although arrays are also objects). You'd write what you have like this:

var domainme = [
    {domain: "one.com", database: "one", api: "one.php"},
    {domain: "two.com", database: "two", api: "two.php"},
    {domain: "three.com", database: "three", api: "three.php"}
];

[] defines an array literal, {} defines an object literal.

Upvotes: 7

Related Questions