2017-06-05 50 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; 
});