2017-11-06 12 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以内にある場合は、2つのスパンを一緒にマージします。ここで

私は今(動作していない)したものであるsee fiddle

まず
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

私たちは何度も繰り返し、すべての要素をチェックする必要がないように、私はスパンをソートします:

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

今、私たちは簡単に通過し、マージすることができます:

ソートされたデータでは

Try it

+0

ありがとう!彼らが私が要求したような> 3を考慮に入れたので、私は他の答えに行った。 – Mcestone

+0

@mcestoneえええええええええええええええええええええええええええええええ –

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);

関連する問題