push
let array = ['강북구', '강남구', '강동구']
array.push('강서구')
console.log(array)
forEach
let newArray = ['윤승근', '고효민', '김지호', '나민지']
newArray.forEach((people) => {
console.log(people)
})
indexOf
let newArray = ['윤승근', '고효민', '김지호', '나민지']
let nameIndex = newArray.indexOf('고효민')
console.log(nameIndex)
findIndex
- 배열 안 객체를 찾거나 조건으로 찾을 시 순서 출력
let peopleInfo = [
{
id : 1,
name : '윤승근',
age : 28
},
{
id : 2,
name : '고효민',
age : 28
},
{
id : 3,
name : '김지호',
age : 28
},
{
id : 4,
name : '나민지',
age : 28
}
]
let newIndex = peopleInfo.findIndex(find => find.name === '김지호')
console.log(newIndex)
find
- 배열 안 객체를 찾거나 조건으로 찾을 시 값 출력
let peopleInfo = [
{
id : 1,
name : '윤승근',
age : 28
},
{
id : 2,
name : '고효민',
age : 28
},
{
id : 3,
name : '김지호',
age : 28
},
{
id : 4,
name : '나민지',
age : 28
}
]
let newIndex = peopleInfo.find(find => find.name === '김지호')
console.log(newIndex)
{ id: 3, name: '김지호', age: 28 }
filter
- 조건에 해당하는 데이터를 찾아 새로운 배열 생성
let peopleInfo = [
{
id : 1,
name : '윤승근',
age : 28
},
{
id : 2,
name : '고효민',
age : 28
},
{
id : 3,
name : '김지호',
age : 28
},
{
id : 4,
name : '나민지',
age : 28
}
]
let newPeopleInfo = peopleInfo.filter(find => find.name === '나민지')
console.log(newPeopleInfo)
splice
let array = ['윤승근', '고효민', '김지호', '나민지']
let index = array.indexOf('고효민')
let newArray = array.splice(index, 2)
console.log(array)
console.log(newArray)
slice
- 배열의 원소 삭제
- splice 함수와는 다르게 기존 배열의 반영 안됨
let array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let newArray = array.slice(0, 5)
console.log(newArray)
shift
- 배열의 처음 원소 출력
- 기존 배열의 결과 반영
let array = ['강북구', '강남구', '강동구', '강서구']
let arrayValue = array.shift();
console.log(arrayValue)
console.log(array)
unshift
let array = ['c#', 'java', 'python', 'swift']
let arrayValue = array.unshift('javascript');
console.log(array)
pop
- 배열의 마지막 원소 출력
- 기존 배열의 결과 반영
let array = ['강북구', '강남구', '강동구', '강서구']
let arrayValue = array.pop();
console.log(arrayValue)
console.log(array)
concat
- 두개의 배열을 하나로 합침
- 기존 배열의 영향 없음
let arrayOne = ['강북구', '도봉구', '노원구']
let arrayTwo = ['강남구', '송파구', '강동구']
let newArray = arrayOne.concat(arrayTwo);
console.log(newArray)
join
let array = ['windows', 'ios', 'ubuntu']
let newArray = array.join();
console.log(newArray)
let newArray2 = array.join((' '))
console.log(newArray2)