配列 Array
scores = [87 , 78, 37]
Array [ 87, 78, 37 ]
scores.push(90). …追加
4
=====================================
console.log(scores)
Array(4)[ 87, 78 , 37, 90]
scores.length ……配列の項目数を表記
4
======================================
scores.push("わーい") …追加
5
scores.push(true) …追加
6
console.log(scores) …配列全体を表示
Array [ 87, 78, 37, 90, わーい, true]
=======================================
scores.shift() …最初の項目を削除
87 (…が削除された)
scores.pop() …最後の項目を削除
true (…が削除された)
=======================================
console.log(scores)
Array [78, 37, 89,わーい]
オブジェクト
配列と同様に複数のデータを管理する キー(プロパティ)で管理する
const scores = { math:87, science:34, english:67 } defined
=======================================================================
console.log(scores)
Object{ english:67,math:87,science:34 }
scores['english']
67
scores.english
67. この入力方法がよく使われる
========================================================================
scores.english = 0. …英語を0点に変更
0
console.log(scores)
Object { english:0,math:87,science:34 }
=========================================================================
const allscores =[ {math: 67, science: 56, english:90}, {math: 89, science: 32, english:23} ]
allscores[0] Object{ english:90,math:67,science:56 }
allscores[1] Object{ english:23,math:89,science:32 }
=========================================================================
allscores[1].english
23
オブジェクトにはfunction(関数)を入れ込める
const scores = { math : 20, message: function(){ console.log("メッセージだよ") } }
scores.message()
メッセージだよ
const user = { firstName: 'taro', lastName: 'yamada', fullname: function () { return `${this.lastName} ${this.firstName}` },
user.fullname()
yamada taro
const user = {
firstName: 'taro',
lastName: 'yamada',
fullname: function () {
return `${this.lastName} ${this.firstName}`
},
sayHi:function() {
const fullname = this.fullname()
console.log(`${fullname}だよ`)
}
}
user.sayHi()
yamada taroだよ
繰り返し forech
const scores = [1,2,3,45,6,66,6]
for(let score of scores) {
console.log(score)
}
1
2
3
45
6
66
6
for (let count = 0; count < 5; count++) {
console.log(count)
}
0
1
2
3
4
scores.forEach(function (score) {
console.log(score)
})
1
2
3
45
6
66
6