# 어레이 함수 문제 답 ``` javascript= 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” 제거하기 animals.pop("Zebra"); console.log(animals); // 주어진 어레이에 “Dog” 추가하기 animals.push("Dog"); console.log(animals); // 주어진 어레이에 “Mosquito”,“Mouse”,“Mule” 추가하기 animals.push("Mosquito", "Mouse", "Mule"); console.log(animals); // 해당 어레이에는 "Human"이 있는가? console.log(animals.includes("Human")); // false // 해당 어레이에는 “Cat” 이 있는가? console.log(animals.includes("cat")); // false // "Red deer"을 "Deer"로 바꾸시오 animals[animals.indexOf("Red deer")] = "Deer"; // animals[77] = "Deer" console.log(animals); // "Spider"부터 3개의 아이템을 기존 어레이에서 제거하시오 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); ``` ## filter() ``` javascript= // animal의 배열에 있는 요소에서 "B"로 시작하는 모든 요소들을 필터링하여 만든 배열 let filterList = animals.filter((animal) => animal.startsWith("B")); console.log(filterList); // animal의 배열에 있는 요소에서 "s"로 끝나는 모든 요소들을 필터링하여 만든 배열 let filterList = animals.filter((animal) => animal.endsWith("s")); console.log(filterList); // "B" 또는 "b"로 시작하는 모든 요소 필터링 let filteredListStartsWithB = animals.filter(animal => animal.startsWith("B") || animal.startsWith("b")); console.log(filteredListStartsWithB); // 주어진 배열에 이름에 "hi"가 포함된 모든 동물들의 이름 let filterList = animals.filter((animal) => animal.includes("hi")); console.log(filterList); // JavaScript에서 기본적으로 문자열 비교는 대소문자를 구분함 ``` ## 다양한 사용법 참고 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice