0
0

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.

オブジェクトの基礎

0
Last updated at Posted at 2020-12-24

オブジェクトを定義する

{プロパティ1:値1,プロパティ2:値2}

・{}(ブレース)で囲む。
・プロパティと値は:(コロン)で繋ぐ。
・要素と要素は,(コンマ)で区切る。

オブジェクトの値を取り出す

```javascript オブジェクト.プロパティ名 ```
example.js
const item = {name:"ブラウス",price:2000};
console.log(item.price)
console
2000

オブジェクトの値を更新

```javascript オブジェクト.プロパティ名 = 新しい値 ```
example.js
const item = {name:"ブラウス",price:2000};
item.price = 1800;
console.log(item.price)
console
1800

配列にオブジェクトを代入する

```javascript [{プロパティ1:値1...},{プロパティ2:値2...}] ```
example.js
const items = [
  {name:"ブラウス",price:2000}, //オブジェクト1。カンマで区切る
  {name:"デニム",price:5000}   //オブジェクト2
]

配列の中のオブジェクトを取り出す

```javascript:example.js const items = [ //配列の要素にはインデックス番号が振り分けられている。 {name:"ブラウス",price:2000}, // インデックス番号⓪ {name:"デニム",price:5000} // インデックス番号① ]

//配列itemのインデックス番号1を取り出す
console.log(items[1]);

//配列の中のオブジェクトの値を取り出す
配列[インデックス番号].プロパティ名

console.log(items[1].price);


```console:console
{name:"デニム",price:5000}
5000

複雑なオブジェクトを扱う

オブジェクトの値の部分にさらにオブジェクトを用いる。
example.js
const cafe = {
  name:スターバックスコーヒー,
  bussinessHours {
    opening:"10時00分",
    closing:"21時00分",
  },
  menu:["ブラックコーヒー","カフェラテ","ココア"]
};

console.log(cafe.name);
console.log(`営業時間:${cafe.bussinessHours.opening}から${cafe.bussinessHours.closing}`)
console
スターバックスコーヒー
営業時間:10時00分から21時00分
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?