LoginSignup
0
0

More than 3 years have passed since last update.

JavaScriptの配列とオブジェクト(基礎)

Posted at

JavaScriptの基本

コピペして使ってください。
ES6を対象としています。

配列

sample.js
//配列の宣言
const animals = ["dog", "cat", "pig"];
//配列の要素を取得
console.log(animals[0]);

//配列の繰り返し
for (let i = 0; i < animals.length; i++) {
  console.log(animals[i]);
}

オブジェクト

sample.js
//オブジェクトの宣言
const character = {name: "John", age: 22};
//オブジェクトの要素を取得
console.log(character.name);

オブジェクトの要素をもつ配列

sample.js
//オブジェクトの要素をもつ配列の宣言
const characters = [
  {name: "John", age: 22},
  {name: "Kevin", age: 23}
];
//要素を取得
console.log(characters[0]);
console.log(characters[1].name);

//繰り返し処理
for (let i=0; i<characters.length; i++) {
  const character = characters[i];
  console.log(`名前:${character.name}`);
  console.log(`年齢:${character.age}`);    
}
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