삭제를 할때에는 delete 보다 slice 를 사용해야 완전히 삭제가 된다.
ch03_test13.js
var users=[{name:'소녀시대', age:20}, {name:'걸스데이', age:22}, {name:'티아라', age:21}]; delete users[1]; console.dir(users); console.log("------------------------------"); users.forEach(function(elem, index){ console.log('원소 #' + index) ; console.dir(elem) ; }); console.log("------------------------------"); //1 - 두번째 , 0 추가 , 객체 users.splice(1, 0, {name: '애프터스쿨', age:24} ); console.log("users 길이 : " + users.length); console.log("------------------------------"); //splice 삭제 //두번째에서 시작해서 1개를 삭제한다. users.splice(2, 1); console.dir(users);
Command: node "C:/Program Files (x86)/Brackets/www/macaronics/ch03_test13.js"
Working directory: C:/Program Files (x86)/Brackets/www/macaronics/
[ { name: '소녀시대', age: 20 },
<1 empty item>,
{ name: '티아라', age: 21 } ]
------------------------------
원소 #0
{ name: '소녀시대', age: 20 }
원소 #2
{ name: '티아라', age: 21 }
------------------------------
users 길이 : 4
------------------------------
[ { name: '소녀시대', age: 20 },
{ name: '애프터스쿨', age: 24 },
{ name: '티아라', age: 21 } ]
Program exited with code 0
ch03_test14.js
var users=[{name:'소녀시대', age:20}, {name:'걸스데이', age:22} , {name:'티아라', age:11}]; var users2=users.slice(1,2); console.log('users 객체'); console.dir(users); console.log("---------------------------------"); console.log('users2 객체'); console.dir(users2); console.log("---------------------------------"); console.dir(users);
Command: node "C:/Program Files (x86)/Brackets/www/macaronics/ch03_test14.js"
Working directory: C:/Program Files (x86)/Brackets/www/macaronics/
users 객체
[ { name: '소녀시대', age: 20 },
{ name: '걸스데이', age: 22 },
{ name: '티아라', age: 11 } ]
---------------------------------
users2 객체
[ { name: '걸스데이', age: 22 } ]
---------------------------------
[ { name: '소녀시대', age: 20 },
{ name: '걸스데이', age: 22 },
{ name: '티아라', age: 11 } ]
Program exited with code 0
댓글 ( 4)
댓글 남기기