2014-05-14 6 views
0

배열을 반복하여 속성이 일부 조건과 일치하는지 확인하고 조건이 적용되는 인덱스를 검색하고 jquerys map 메서드를 사용하려고 생각했습니다. 내 문제 here에 대한 정확한 해결책을 찾은 후 동일한 문제가 발생하면 물건을 만들기 위해 내 coffeescript를 굴렸다.TypeError : obj is undefined Coffeescript

buildNewWaypointsFromDraggable:()-> 
    [email protected][0] 
    indexes = $.map(@newDraggableWaypoints, (obj, index) => 
     index if obj.legStart is item 
    ) 
    window.alert indexes.toString() 

왜 이것이다 : @newDraggableWaypoints.push newWaypoint

이 코드는 나에게 headeache을 제공 이잖아입니다 :

newWaypoint = {waypoint:latlon.toString(), legStart:leg.start_location.toString() ,legNumber:legNo.toString()} 

와 같은 객체가 간단한 배열에 밀려 :

내 목적은 이것이다 메시지가 표시 되었습니까? 조건이 충족되는 경우 index 및 다른 undefined : 당신의 map배열의 모든 요소에 대한 뭔가를 검색하지, 당신

답변

1

retrieve the indexes where the condition applies

더 감사드립니다. 일부 요소를 제외하려면 filter을 사용해야합니다. 당신은 할 수

indexes = @newDraggableWaypoints.map (obj, index) => 
    index if obj.legStart is item 
.filter (obj) -> 
    obj is not undefined 

또는

indexes = @newDraggableWaypoints 
    .map (obj, index) -> [obj, index] 
    .filter ([obj, i]) -> obj.legStart is item 
    .map ([o, index]) -> index 
관련 문제