본문 바로가기
Computer Science/Javascript

[Javascript ] 자바스크립트 배열 맛보기, All About Javascript Arrays

by 기억보다 기록을 2023. 4. 2.
반응형

JavaScript 

  • JavaScript는 웨페이지에서 복잡한 기능을 구현할 수 있도록 하는 객제현 스크린팅 프로그램밍 언어입니다.
  • 자바스크립트는 동적이며, 타입을 명시할 필요가 없는 인터프리터 언어입니다.
  • 자바스크립트는 객체 지향형 프로그래밍과 함수형 프로그래밍을 모두 표현할 수 있습니다.
  • Interpreted Language as JavaScript, Python Ruby
  • Compiled Languages: Java, C/C++, Swift 

 

 

 

Arrays


  • Arrays : a net way of strong a list items under a single variable name.
  • Arrays are generally described as "list-like objects"
  • Basically single objects that contain multiple values stored in a list.
  • Can mix several data types string, number, objects
const array = [1, "Hello", [0,1,2]]

 

  • Finding the length of a array : using array.length property.
  • Start with 0, list number called index.
  • Here interesting fact computers start count with 0;

 

  • Array inside array called : Multidimensional Array 
  • Special Point of Multidimensional arrays two sets of brackets []
const array[1, "Hello", [0,1,2]]
array[0] = 1 and array[1] is "Hello".
array[2][0] equal fist value of fist index as 0
array[2][1] is returns 1 and array[2][2] is returns 2

  • Finding the index of items in an array 
  • index.of() method takes an item argument or returns "-1" when value not included
const fruit = ["banana", "apple", "orange", "kiwi"];

console.log(fruit.indexOf('banana')) => returns 1
console.log(fruit.indexOf('mango')) => returns -1, Not included

  •  Adding Items in an array
  • Add one or more items to the end of an array we use push();
const num = [1,2,3]; // length = 3

num.push(5) // [1,2,3,5], length = 4

 

  • Adding Items in start of array using unshift();
const num = [1,2,3];

num.unshift(5); // [5,1,2,3]

 

  •  Removing items last item use pop();
const num = [1,2,3,5,70];

num.pop(); // [1,2,3,5];

  • The pop() method returns the deleted item.
const num = [1,2,3,5,70];
var deletedItem = num.pop();

console.log(deletedItem);  // 70

  • To Remove the first item of array use shift();
const num = [1,2,3,4];

num.shift(); // [2,3,4];

 

  •  To Remove nth item in array using splice();
  •  In this call splice(a,b) => a is as fist argument where to start removing and b shows how many items will delete 
  •  if  (index != -1) =>  index == -1 means that not included, so index != -1 must include
         
const num = [1,2,3,4,70,100];

   const index = num.indexOf(3);
         if(index != -1){
            num.splice(index,2)
            
            // as splice() method here "index" is 2th as 3 
            // and "2" means will delete 2 items start 2th index that 3
            
         }

console.log(num); // [1,2,70,100], length is 4

  • Accessing every item , using for .. of method
const num = [1,2,70,100];

      for(const getIndex of num){
         console.log(getIndex);
         
         // result get all of index values separately
}

 

 

  • map() method
  • map() method calls the function once for each item in the array, passing in the item then adds the return value from each function call to a new array and returns the new array.
 function double(num) { 
      return num * 2 
 }

  const num = [1,2,3,4,5];
  const result = num.map(double);
  
  console.log(result); // [2,4,6,8,10]

  • filter() method
  • Using filter() we can create new arrays with conditions below the array.
  • filter() calls for every item in the array, if function is true then add to a new array.
 function isLong(name) { 
    return name.length > 3
    // get items legth more than 3
 }

 const arr = ["test", "abc", "apple", "it", "a"];
 const result = arr.filter(isLong);

 onsole.log(result);  // ['test', 'apple']

 

  • split() method
  • The split() method divides a string into a list of substrings and returns them as an array.
  •  The split() method takes a single parameter, the character we want to separate.
  • Also toString() converting an array to string
  • Syntax :  string.split(separator, limit)
const message = "JavaScript::is::fun";

// divides the message string at ::

let result = message.split("::");
console.log(result);

// Output: [ 'JavaScript', 'is', 'fun' ]

 

반응형