2017-12-01 9 views
0

Lodash으로 제거하고 싶습니다. 개체는 uuid : xxx이며 xxx2가 아닙니다.Lodash로 개체를 제거하는 방법 (찾기 및 제거)

내가 어떻게 만들 수 있습니까? Lodash?

[ 
    { 
     "uuid": "xxx", 
     "name": "test" 
    }, 
    { 
     "uuid": "xxx2", 
     "name": "test 2" 
    } 
] 

내 실제 코드 :

_.forEach(this.objs, (obj, index, collection) => { 
    _.remove(obj, {uuid}) 
}) 
+0

필터링'도착 = arr.filter 사용 (! (UUID {}) => UUID를 == 'XXX') 배열 [분리 요소 ' –

+0

가능한 복제 Lodash 사용하기] (https://stackoverflow.com/questions/28036716/removing-elements-in-an-array-using-lodash) –

+0

새 배열 또는 _.remove를 반환하여 _.filter를 사용하면 위치를 변경할 수 있습니다. . –

답변

1

당신이 할 수있는 간단한 사용 _.remove 방법에 대한 Array.prototype.filter()를 사용할 수 있습니다.

var objects = [ 
 
    { 
 
     "uuid": "xxx", 
 
     "name": "test" 
 
    }, 
 
    { 
 
     "uuid": "xxx2", 
 
     "name": "test 2" 
 
    } 
 
]; 
 

 
_.remove(objects, o => o.uuid === 'xxx2'); 
 

 
console.log(objects);
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

+0

'_.remove (objects, {uuid : 'xxx2'}); ' –

+0

Doesn –

+0

@ CélineMartin 제 답변에서'Run code snippet' 버튼을 누르십시오. 또는 당신의 예상 결과를 줘? –

0

당신은 그

let arr = [ 
 
    { 
 
     "uuid": "xxx", 
 
     "name": "test" 
 
    }, 
 
    { 
 
     "uuid": "xxx2", 
 
     "name": "test 2" 
 
    } 
 
] 
 

 

 
arr = arr.filter(e => e.uuid != 'xxx2'); 
 

 
console.log(arr);