11日目TIL-wecode
16014 ワード
'use strict';
//Array
//1. Declaration
const arr1 = new Array();
const arr2 = [1, 2];
//2. Index position
const fruits = ['apple', 'banana'];
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();
//3. Looping over an array
//print all fruits
//a. for
for(let i = 0; i < fruits.length; i++){
console.log(fruits[i]);
}
//b. for...of
for (let fruit of fruits){
console.log(fruit);
}
//c. forEach
fruits.forEach(function (fruit, index){
console.log(fruit, index);
});
//더깔끔하게
fruits.forEach((fruit) => console.log(fruit));
//4. Addition, deletion, copy
//push : add an item to the end
fruits.push('straw','peach');
console.log(fruits);
//pop : remove an item from the end
fruits.pop();
console.log(fruits);
//unshift : add an item to the beginning
fruits.unshift('straw', 'lemon');
console.log(fruits);
//shift : remove an item to the beginning
fruits.shift();
console.log(fruits);
//note!! shift, unshift are slower than pop, push
//splice : remove an item by index position
fruits.push('straw', 'peach', 'lemon');
console.log(fruits);
fruits.splice(1, 1); //1 index부터 1 개의 index를 지운다
console.log(fruits);
fruits.splice(1, 1, 'greenApple', 'watermelon'); //1index에 1개를 지우고 뒤에 두개가 그자리에 들어간다
console.log(fruits);
//conbine two arrays
const fruits2 = ['mogwa', 'coconut'];
const newFruits = fruits.concat(fruits2);
console.log(newFruits);
//5. searching
//find the index
console.clear();
console.log(fruits);
console.log(fruits.indexOf('apple'));
console.log(fruits.indexOf('waqtermelon'));
console.log(fruits.indexOf('coconut'));
//includes
console.log(fruits.includes('watermelon'));
console.log(fruits.includes('coconut'));
//lastIndexOf
console.clear();
fruits.push('apple');
console.log(fruits);
console.log(fruits.indexOf('apple'));
console.log(fruits.lastIndexOf('apple'));
Reference
この問題について(11日目TIL-wecode), 我々は、より多くの情報をここで見つけました https://velog.io/@kyj2471/TIL-wecode-11일차テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol