728x90
* slice๋ ๊ธฐ์กด์ ๋ฐฐ์ด์ ๊ฑด๋ค์ง ์๋๋ค.(๋ฐ๋ผ์ ์๋ฆฐ ๊ฐ์ ๋นผ์ ์ ๋ฐฐ์ด์ ๋ง๋ค๊ณ ์ถ์ผ๋ฉด let ์ ๋ณ์์ ํ ๋นํด์ผํจ)
* splice๋ ๊ธฐ์กด์ ๋ฐฐ์ด์ด ์๋ฆฐ๋ค.
* push() ๋ฐฐ์ด ๋ท๋ถ๋ถ์ ์์ ์ถ๊ฐ
* ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํด ๋ฐฐ์ด ๋ท๋ถ๋ถ์ ์์ ์ถ๊ฐํ๊ธฐ
const fruit =['๋ฐ๋๋','์ฌ๊ณผ','๋ฐฐ']
fruit[fruit.length]='๊ทค'
fruit
['๋ฐ๋๋','์ฌ๊ณผ','๋ฐฐ','๊ทค']
์๋ฐ ๊ฐฌ์ฑ..,
let fruit1="banana"
let fruit2="apple"
let fruit3="grape"
let fruits = ["banana","apple","grape","mango"]
console.log(fruits)
console.log(fruits[0])
console.log(fruits[1])
console.log(fruits[2])
console.log(fruits[3])
console.log(fruits[4])
fruits[4] = "cherry"
console.log(fruits[4])
console.log(fruits)
//pop() : ๋ฐฐ์ด ๋ง์ง๋ง์ ์๋ ์์ ์ ๊ฑฐ
fruits.pop()
console.log(fruits)
//push() : ๋ฐฐ์ด ๋ง์ง๋ง์ ์๋ก์ด ์์ ์ถ๊ฐ
fruits.push("orange")
console.log(fruits)
//includes("์์"): ๋ฐฐ์ด์ ํฌํจ๋์ด ์๋ค true
//includes("์์"): ๋ฐฐ์ด์ ์ ํฌํจ๋์ด ์๋ค false
console.log(fruits.includes("water"))
console.log(fruits.includes("orange"))
//indexOf("์์"): ์์ดํ
์ ์ธ๋ฑ์ค ๋ฒํธ๋ฅผ ๋ฆฌํด
console.log(fruits.indexOf("orange"))
console.log(fruits.indexOf("water"))//์๋ ์์๋ -1 ์ถ๋ ฅ
//slice(์์์ , ๋์ ): ์์์ ~๋์ (๋ฏธํฌํจ) ๊น์ง ๋ฐฐ์ด์ ๋ณต์ฌํด์ ๋ฆฌํด
console.log(fruits.slice(1))
console.log(fruits.slice(1,4))
console.log(fruits.slice(1,3))
์ถ๋ ฅ๊ฒฐ๊ณผ
[ 'banana', 'apple', 'grape', 'mango' ]
banana
apple
grape
mango
undefined
cherry
[ 'banana', 'apple', 'grape', 'mango', 'cherry' ]
[ 'banana', 'apple', 'grape', 'mango' ]
[ 'banana', 'apple', 'grape', 'mango', 'orange' ]
false
true
4
-1
[ 'apple', 'grape', 'mango', 'orange' ]
[ 'apple', 'grape', 'mango' ]
[ 'apple', 'grape' ]
๋ฐฐ์ด๊ณผ ๊ฐ์ด ์ธ ์ ์๋ ํจ์๋ค ๐
- pop() : ๋ฐฐ์ด ๋์์๋ ์์ดํ ์ ์ ๊ฑฐ, ๊ทธ ์์ดํ ๊ฐ์ ๋ฆฌํด
- push(‘์์ดํ ’): ๋ฐฐ์ด ๋์ ์์ดํ ์ถ๊ฐ, ๋ฐฐ์ด์ ์ต์ข ๊ธธ์ด ๋ฆฌํด
- includes(‘์์ดํ ’): ๋ฐฐ์ด์ ์์ดํ ์ด ํฌํจ๋์ด์์ผ๋ฉด true๋ฆฌํด ์๋๋ฉด false ๋ฆฌํด
- indexOf(‘์์ดํ ’): ์์ดํ ์ ์ธ๋ฑ์ค ๋ฒํธ๋ฅผ ๋ฆฌํด
- slice(์์์ , ๋์ ):
์์์ ~๋์ (๋ฏธํฌํจ) ๊น์ง ๋ฐฐ์ด์ ๋ณต์ฌํด์ ๋ฆฌํดlet extrafruits = fruits.slice(1,3) consoe.log(extrafruit)
์ข๋ ๋ค์ํ ์ฌ์ฉ๋ฒ ์ฐธ๊ณ - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice - splice(์์์ ,๊ฐ์): ์์์ ๋ถํฐ ๋ช๊ฐ์ ์์๋ฅผ ์ ๊ฑฐํ๊ณ ์ถ์์ง
fruits.splice(2,1) console.log(fruits)
- length : ๋ฐฐ์ด ํจ์๋ ์๋์ง๋ง ๋ฐฐ์ด์ ํฌ๊ธฐ๋ฅผ ๋ฆฌํดํด์ฃผ๋ ์์ฑ
์์
let array = ["apple","banana","mango"]
console.log(array.length) // 3์ด ๋์ด, ๋ฐฐ์ด์ ์์ดํ ์ด ์ด 3๊ฐ๋ผ์
<๋ฐฐ์ด ์ฐจ๋ก๋๋ก ์ถ๋ ฅํ๊ธฐ>
* ์๋ฆฌ๋ ์์ ์บก์ฒ์ ๋๋ค.
728x90
'Javascript > Javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๊ธฐ์ด ์๋ฐ์คํฌ๋ฆฝํธ] 6. if๋ฌธ, ์ผํญ์ฐ์ฐ์ (0) | 2022.03.09 |
---|---|
[๊ธฐ์ด ์๋ฐ์คํฌ๋ฆฝํธ] 5. ๊ฐ์ฒด ํ์ (์๋ฃํ), ๊ฐ์ฒด(์๋ฃํ)๋ฅผ ๋ฐฐ์ด ์์ ๋ฃ๊ธฐ (0) | 2022.03.09 |
[๊ธฐ์ด ์๋ฐ์คํฌ๋ฆฝํธ] 3. temp ์์ (0) | 2022.03.03 |
[๊ธฐ์ด ์๋ฐ์คํฌ๋ฆฝํธ] 2. ์๋ฃํ๊ณผ ์ฐ์ฐ์ (0) | 2022.03.03 |
[๊ธฐ์ด ์๋ฐ์คํฌ๋ฆฝํธ] 1. ๋ณ์ ์ ์ธ (let, const, var) (0) | 2022.03.03 |