7. JS basic (Array - Looping over an array, (Addition, Deletion, Copy), Combine two arrays, Searching)

Changmok LEE·2023년 2월 7일

Array

1. Looping over an array

const arr = new Array();
const fruits = ['apple', 'banana'];
console.log(fruits.length);   // 2

// 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((fruit, index) => { // 마지막 요소로 array 받아올 수 도 있음
	console.log(fruit, index);
});
==
fruits.forEach((fruit, index) => console.log(fruit, index));
}
  • forEach()

    forEach는 콜백 함수를 반환
    인자와 index, 배열을 반환한다
    배열의 인자마다 내가 전달한 function을 출력한다

    /**
         * Performs the specified action for each element in an array.
         * @param callbackfn  A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
         * @param thisArg  An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
         */
        forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void;

2. Addition, Deletion, Copy

push : add an item to the end
pop : remove an item from the end
unshift: add an item to the beginning
shift: remove an item from the beginning
splice : remove an item by index position

// unshift
fruits.unshift('mango');
console.log(fruits); // ['mango', 'apple', 'banana']

// shift
fruits.shift();
console.log(fruits); // ['apple', 'banana']
  • note!!!

    shift, unshift are slower than pop, push
    overhead 현상 발생

splice : remove an item by index position

  • 지정된 index 값 삭제
/**
     * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
     * @param start The zero-based location in the array from which to start removing elements.
     * @param deleteCount The number of elements to remove.
     * @returns An array containing the elements that were deleted.
     */
    splice(start: number, deleteCount?: number): T[]; // ?는 optional
fruits.splice(1); // index 1번 이후부터 전부 삭제
fruits.splice(1, 1); // index 1번 요소 하나만 삭제
fruits.splice(1, 1, 'grape', 'orange'); // index 1번 요소 삭제 후 삽입 가능 == ['apple', 'grape', 'orange'] (banan 삭제 후 삽입)

3. Combine two arrays

concat : Combines two or more arrays

/**
     * Combines two or more arrays.
     * This method returns a new array without modifying any existing arrays.
     * @param items Additional arrays and/or items to add to the end of the array.
     */
    concat(...items: ConcatArray<T>[]): T[]; // 배열을 return
const fruits = ['apple', 'grape', 'orange'];
const fruits2 = ['strawberry', 'mango'];
const newFruits = fruits.concat(fruits2);
console,log(newFruits); // ['apple', 'grape', 'orange', 'strawberry', 'mango']

4. Searching

  • indexOf == find the index
  • includes == return true / false
  • lastIndexOf == find the last index
const fruits = ['apple', 'grape', 'orange'];

console.log(fruits.indexOf('apple')) // 0
console.log(fruits.indexOf('orange')) // 2
console.log(fruits.indexOf('A')) // -1
console.log(fruits.includes('orange')) // true

fruits.push('apple');
console.log(fruits.lastIndexOf('apple')); // 3
profile
이창목

0개의 댓글