배열함수

Jul 09, 2024
배열함수

목차

let animals= [ "Aardvark", "Albatross", "Alligator", "Alpaca", "Ant", "Ape", "Armadillo", "Donkey", "Baboon", "Badger", "Barracuda", "Bat", "Bear", "Beaver", "Bee", "Bison", "Cat", "Caterpillar", "Cattle", "Chamois", "Cheetah", "Chicken", "Chimpanzee", "Chinchilla", "Chough", "Clam", "Cobra", "Cockroach", "Cod", "Cormorant", "Dugong", "Dunlin", "Eagle", "Echidna", "Eel", "Eland", "Elephant", "Elk", "Emu", "Falcon", "Ferret", "Finch", "Fish", "Flamingo", "Fly", "Fox", "Frog", "Gaur", "Gazelle", "Gerbil", "Giraffe", "Grasshopper", "Heron", "Herring", "Hippopotamus", "Hornet", "Horse", "Kangaroo", "Kingfisher", "Koala", "Kookabura", "Moose", "Narwhal", "Newt", "Nightingale", "Octopus", "Okapi", "Opossum", "Quail", "Quelea", "Quetzal", "Rabbit", "Raccoon", "Rail", "Ram", "Rat", "Raven", "Red deer", "Sandpiper", "Sardine", "Sparrow", "Spider", "Spoonbill", "Squid", "Squirrel", "Starling", "Stingray", "Tiger", "Toad", "Whale", "Wildcat", "Wolf", "Worm", "Wren", "Yak", "Zebra" ]
 

어레이의 마지막 아이템 ‘zebra’ 제거

1. Array에 마지막 아이템 'Zebra'를 제거 animals.pop() // 객체.pop() 마지막 아이템 제거
 

주어진 어레이의 ‘Dog’ 추가

animals.push('Dog')

주어진 어레이에 ‘Mosquito’, ‘Mouse’, ‘Mule’을 추가하기

animals.push("Mosquito","Mouse","Mule")

해당 어레이에는 "Human"이 있는가?

animals.includes("Human")

해당 어레이에는 “Cat” 이 있는가?

animals.includes("Cat")

"Red deer"을 "Deer"로 바꾸시오

animals.indexOf("Red deer") =>몇번 째 있는지 확인할 수 있게 됨 animals[animals.indexOf("Red deer")]=> animals[77]="Deer"
 
slice(시작점, 끝점): 시작점~끝점(미포함) 까지 배열을 복사해서 리턴
splice(시작점,개수): 시작점부터 개수만큼 실제 배열에서 아이템 제거
 

"Spider"부터 3개의 아이템을 기존 어레이에서 제거하시오

기존에 어레이에서 제거하라 => splice animals.splice(animals.indexOf("Spider"),3) console.log(animals)
 

"Tiger"이후의 값을 제거하시오

animals.splice(animals.indexOf("Tiger")) console.log(animals)

"B"로 시작되는 아이템인 "Baboon"부터 "Bison"까지 가져와 새로운 어레이에 저장하시오

let newList = animals.slice(animals.indexOf("Baboon"),animals.indexOf("Bison")+1) console.log(newList)
 
Share article

정리한 노션 내용을 올리는 공간