2017-11-06 1 views
1

나는이 다음과 같습니다 배열 :객체 내에서 숫자의 친밀감을 기준으로 배열을 줄

[ 
    { "begin": 870, "end": 889, "spanType": ["plan", "gt-plan"] }, 
    { "begin": 890, "end": 925, "spanType": ["plan", "gt-plan"] }, 
    { "begin": 926, "end": 938, "spanType": ["plan", "gt-plan"] }, 
    { "begin": 939, "end": 958, "spanType": ["plan", "gt-plan"] }, 
    { "begin": 7732, "end": 7790, "spanType": ["plan", "gt-plan"] }, 
    { "begin": 7791, "end": 7879, "spanType": ["plan", "gt-plan"] } 
] 

나는이를 통해 루프를 필요로하고 다음과 같습니다 배열을 만들 :

[ 
    { "begin": 870, "end": 958, "spanType": ["plan", "gt-plan"] }, 
    { "begin": 7732, "end": 7879, "spanType": ["plan", "gt-plan"] } 
] 

기본적으로 span.end가 다음 span.begin 중 3 이내이면을 병합하십시오. 우리는 반복해서 모든 요소를 ​​점검 할 필요가 없도록

spans.forEach(function(d,i) { 
    if (i+1 <= spans.length - 1) { 
    if (spans[i+1].begin <= d.end + 3) { 
    d.end = spans[i+1].end; 
    newSpans.push(d); 
    } 
    else { 
     newSpans.push(spans[i]); 
    } 
    } 
}); 

see fiddle

+1

사용'reduce' 대신? – evolutionxbox

+0

'spanType'의 동일성을 검사해야합니까? –

답변

1

, 당신은 실제 요소와 마지막으로 삽입 된 요소를 확인할 수 있으며, 델타가 원하는 수보다 작은 경우, 다음 end 값을 조정합니다.

이 제안은 원래 배열을 변경합니다. 원하지 않으면 푸시 할 때 객체 복사본을 가져와야합니다.

var array = [{ begin: 870, end: 889, spanType: ["plan", "gt-plan"] }, { begin: 890, end: 925, spanType: ["plan", "gt-plan"] }, { begin: 926, end: 938, spanType: ["plan", "gt-plan"] }, { begin: 939, end: 958, spanType: ["plan", "gt-plan"] }, { begin: 7732, end: 7790, spanType: ["plan", "gt-plan"] }, { begin: 7791, end: 7879, spanType: ["plan", "gt-plan"] }], 
 
    result = array.reduce(function (r, o, i) { 
 
     if (!i || o.begin - r[r.length - 1].end >= 3) { 
 
      r.push(o); 
 
     } else { 
 
      r[r.length - 1].end = o.end; 
 
     } 
 
     return r; 
 
    }, []); 
 

 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

+0

감사합니다. – Mcestone

2

이 처음에는 내가 스팬을 정렬 할 것 : 여기

는 지금 (작동하지 않는) see fiddle있는 것입니다 :

spans.sort((a,b) => a.begin - b.begin); 

이제 우리는 쉽게 통과하고 병합 할 수 있습니다 :

정렬 된 데이터와

Try it

+0

감사합니다. 나는 그들이 요청한 것보다 3 이상을 고려했기 때문에 다른 대답을했다. – Mcestone

+0

@mcestone 네 물론'-1'을'-4'로 바꾸고 그걸로 끝내 죠. –

0

const data = [ 
 
    { "begin": 870, "end": 889, "spanType": ["plan", "gt-plan"] }, 
 
    { "begin": 890, "end": 925, "spanType": ["plan", "gt-plan"] }, 
 
    { "begin": 926, "end": 938, "spanType": ["plan", "gt-plan"] }, 
 
    { "begin": 939, "end": 958, "spanType": ["plan", "gt-plan"] }, 
 
    { "begin": 7732, "end": 7790, "spanType": ["plan", "gt-plan"] }, 
 
    { "begin": 7791, "end": 7879, "spanType": ["plan", "gt-plan"] } 
 
]; 
 

 
// your range, representing how close an end has to be to a begin to merge 
 
const RANGE = 3; 
 

 
// iterate through the data still available 
 
for (let i = 0; i < data.length - 1; i++) { 
 
    // check if we should merge the current data object with the next one 
 
    // based on the defined range 
 
    if (data[i].end >= data[i+1].begin - RANGE) { 
 
     // we'll merge the current object into the next one. 
 
     // first, we'll set the begin value of the next object to 
 
     // the one that's being merged into it 
 
     data[i+1].begin = data[i].begin; 
 

 
     // now we push the current object's spanType entries into the next object 
 
     Array.prototype.push.apply(data[i+1].spanType, data[i]); 
 

 
     // finally we remove the current object from the list as it has been 
 
     // fully merged 
 
     data.splice(i, 1); 
 

 
     // we removed an element, so we'll go one back in the list 
 
     i--; 
 
    } 
 
} 
 
console.log(data);

관련 문제