[TIL#4]JavaScriptベース-2



Goal:JavaScriptの基本を学ぶ
Table of Contents
1.差異varletconst2.JavaScriptデータ型
3.配列の運用

3.配列の運用


-アレイ宣言(Declaration)
const arr1 = new Array();
const arr2 = [1, 2];
- Index position
const fruits = ['🍎', '🍌'];
console.log(fruits);
console.log(fruits.length);
console.log(fruits[0]);
console.log(fruits[1]);
console.log(fruits[2]);
console.log(fruits[fruits.length - 1]);
console.clear();
-Looping over an array
print all fruits
  • for
  • for (let i = 0; i < fruits.length; i++) {
      console.log(fruits[i]);
    }
  • for of
  • for (let fruit of fruits) {
      console.log(fruit);
    }
  • forEach
  • fruits.forEach((fruit) => console.log(fruit));
    -Addtion, deletion, copy
  • push: add an item to the end
  • fruits.push('🍓', '🍑');
    console.log(fruits);
  • pop: remove an item from the end
  • const poped = fruits.pop();
    fruits.pop();
    console.log(fruits);
  • unshift: add an item to the benigging
  • fruits.unshift('🍓', '🍋');
    console.log(fruits);
  • shift: remove an item from the benigging
  • fruits.shift();
    fruits.shift();
    console.log(fruits);
    note!! shift, unshift are slower than pop, push
    splice: remove an item by index position
    fruits.push('🍓', '🍑', '🍋');
    console.log(fruits);
    fruits.splice(1, 1);
    console.log(fruits);
    fruits.splice(1, 0, '🍏', '🍉');
    console.log(fruits);
  • combine two arrays
  • const fruits2 = ['🍐', '🥥'];
    const newFruits = fruits.concat(fruits2);
    console.log(newFruits);
    -Searching
  • indexOf: find the index
  • console.clear();
    console.log(fruits);
    console.log(fruits.indexOf('🍎'));
    console.log(fruits.indexOf('🍉'));
    console.log(fruits.indexOf('🥥'));
    - includes
    console.log(fruits.includes('🍉'));
    console.log(fruits.includes('🥥'));
    - lastIndexOf
    fruits.push('🍎');
    console.log(fruits);
    console.log(fruits.indexOf('🍎'));
    console.log(fruits.lastIndexOf('🥥'));
    注意:エリー