2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

開眼!JavaScriptの2.2のまとめ ツリー構造や多次元配列の実現

Last updated at Posted at 2015-04-11

開眼!JavaScriptの2.2のまとめです。

##2.2 オブジェクトと配列でツリー構造や多次元配列を実現
Object()、Array()、Function()オブジェクトは他のオブジェクト(配列を含む)を格納することで、ツリー構造を持ったオブジェクトや多次元配列のようなふるまいを実現することができる。

オブジェクトにオブジェクトを格納して、ツリー構造を実現

var object1 = {
    object1_1:{
        object1_1_1:{foo:'bar'},
        object1_1_2:{}
    },
    object1_2:{
        object1_2_1:{foo:'hoge'},
        object1_2_2:{}
    }
};


console.log(
object1.object1_1.object1_1_1.foo, 
object1.object1_1.object1_1_2, 
object1.object1_2.object1_2_1.foo, 
object1.object1_2.object1_2_2.foo
)

結果(見やすいように改行しています)
bar
Object {}
hoge
undefined

配列を使って擬似的な多次元配列を実現

var myArray = [[[0,1],[2,3]],[[4,5],[6,7]]];
console.log(myArray[0][0], myArray[1][1]);

結果(見やすいように改行しています)
[0, 1]
[6, 7]

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?