Skip to content

Instantly share code, notes, and snippets.

@breakstorm
Created January 10, 2017 13:10
Show Gist options
  • Save breakstorm/587689796f4e04bb05cf6c7bd86ae56f to your computer and use it in GitHub Desktop.
Save breakstorm/587689796f4e04bb05cf6c7bd86ae56f to your computer and use it in GitHub Desktop.
//동일한ID 오브젝트 삭제
var myArr = [
{"id" : 23, "name" : "honux", "content" : "오늘의 커피는 왜 항상 맛있지?", "like" : 2, "comment" : ["^^", "i like this"]},
{"id" : 55, "name" : "nigayo", "content" : "드디어 출근!", "like" : 4, "comment" : ["이직 하셨나봐요? "]},
{"id" : 93, "name" : "jk", "content" : "어제 읽은 책이 아직도 ", "like" : 20, "comment" : ["잠자기 전에 만화책은 금물..", "그게 뭘까?"]},
{"id" : 4, "name" : "crong", "content" : "코드스쿼드가 정말 좋은 곳일까? 믿을 수가 없다..", "like" : 0, "comment" : ["누가 그러디"]}
]
var objSameId = function(e,i,a){
if(a[i].id === index)
delete a[i]
}
var removeContent = function(oldArr,index){
myArr.forEach(objSameId)
}
removeContent(myArr,88)
myArr.forEach(objPrint)
@breakstorm
Copy link
Author

removeContent 함수에 있는 index 값을 objSameId 함수에 전달을 하고 싶습니다.

@honux77
Copy link

honux77 commented Jan 11, 2017

//동일한ID 오브젝트 삭제
var myArr = [
{"id" : 23, "name" : "honux", "content" : "오늘의 커피는 왜 항상 맛있지?", "like" : 2, "comment" : ["^^", "i like this"]},
{"id" : 55, "name" : "nigayo", "content" : "드디어 출근!", "like" : 4, "comment" : ["이직 하셨나봐요? "]},
{"id" : 93, "name" : "jk", "content" : "어제 읽은 책이 아직도 ", "like" : 20, "comment" : ["잠자기 전에 만화책은 금물..", "그게 뭘까?"]},
{"id" : 4, "name" : "crong", "content" : "코드스쿼드가 정말 좋은 곳일까? 믿을 수가 없다..", "like" : 0, "comment" : ["누가 그러디"]}
];

var objSameId = function(removeId, element ,index ,array){
if(array[index].id === removeId)
array.splice(index, 1);
};

var removeContent = function(oldArr,removeId){
this.index = index;
myArr.forEach(objSameId.bind(null, removeId));
};

removeContent(myArr,23);
removeContent(myArr,55);
removeContent(myArr,93);
console.log(JSON.stringify(myArr));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment