2017-06-05 63 views
0

我收到了一堆從socket流和希望重複流合併成一個流(或選擇的第一項)挑的第一個項目重複rxjs

總之我想rxjs做到這一點:

In: 1 - 1 - 1 - 2 - 2 - 3 - 3 - 1 - 1 - 1 - 2 - 2 - 2 - 1 - 2 - 2 - 3 - 3 
Out: 1 - - - - - 2 - - - 3 - - - 1 - - - - - 2 - - - - - 1 - 2 - - - 3 - - 

回答

0

你可以試試這個

var arr = [1,1,1,2,2,3,3,1,1,1,2,2,2,2,1,2,2,3,3]; 
for (i = 1; i < arr.length; i++) { 
    if (arr[i]==arr[i-1]){ arr.splice(i,1);i--;} 
} 

OUT:[1, 2, 3, 1, 2, 1, 2, 3]

0

她e是一個使用Array.fitler()功能的方法:

[1,1,1,2,2,3,3,1,1,1,2,2,2,2,1,2,2,3,3].filter((value, index, src) => { 
    if(index === 0) { 
    //The first element of the array is always included 
    return true; 
    } 
    // Return true (keep) if the current element `value` is different 
    // than the element that was last encountered (`src[index-1]`) 
    return src[index-1] !== value; 
}); 

輸出到這是:

[ 1, 2, 3, 1, 2, 1, 2, 3 ] 

這通過迭代陣列之上並檢查是否每個元素是相同或比上不同在它之前。下面是同樣的事情,但作爲一個班輪:

[1,1,1,2,2,3,3,1,1,1,2,2,2,2,1,2,2,3,3].filter((value, index, src) => { 
    return index === 0 || src[index-1] !== value; 
});