47
37

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)

Posted at

配列

配列とは

複数の値をまとめて管理する。

書き方

要素を [] で囲む

script.js
["apple","banana","orange"]

コンソールに出力

console.log(定数名) で出力

script.js
const fruits = ["apple","banana","orange"];
console.log(fruits);
console
["apple","banana","orange"]

オブジェクト

オブジェクトとは

複数のデータをまとめて管理する。オブジェクトはそれぞれの値にプロパティと呼ばれる名前をつけて管理する。

書き方

要素を {} で囲む
{プロパティ1: 値1,プロパティ2: 値2}

script.js
{name: "apple", price: 200}

コンソールに出力

console.log(定数名) で出力

script.js
const fruits = {name: "apple", price: 200};
console.log(fruits);
console
{name: "apple", price: 200}

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

console.log(定数名.プロパティ名) で出力

script.js
const fruits = {name: "apple", price: 200};
console.log(fruits.name);
console
apple

番外編:オブジェクトを要素に持つ配列

配列の要素には、文字列や数値だけじゃ無くオブジェクトも使うことができる。

script.js
const fruits = [
  {name: "apple", price: 200},
  {name: "banana", price: 100}
];

配列の中のオブジェクトのプロパティの値を取り出す

console.log(定数名[インデックス番号].プロパティ名) で出力

script.js
const fruits = [
  {name: "apple", price: 200},
  {name: "banana", price: 100}
];
console.log(fruits[1].price);
console
100
47
37
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
47
37

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?