2017-09-29 39 views
0

大家好,我有一個問題,我有這樣的濾波器陣列到另一個陣列使用更新的值

array 1 =[ 
    {title: 'yes', time: "00:01:50"}, 
    {title: 'yes', time: "00:02:50"}, 
    {title: 'yes', time: "00:01:50"}, 
    {title: 'no', time: "02:01:00"}, 
    {title: 'yes', time: "02:01:50"}, 
    {title: 'no', time: "10:01:50"}, 
]; 

我想通過標題來過濾此數組中angular2或JS到另一個陣列array2是這樣的一個數組

array2 = [ 
    { 
    title :'yes', 
    count:4, 
    values:[ 
     {title: 'yes', time: "00:01:50"}, 
     {title: 'yes', time: "00:02:50"}, 
     {title: 'yes', time: "00:01:50"}, 
     {title: 'yes', time: "02:01:50"}, 
     ] 
    }, 
    { 
    title :'no', 
    count:2, 
    values:[ 
     {title: 'no', time: "02:01:00"}, 
     {title: 'no', time: "10:01:50"}, 
     ] 
    } 
]; 

其中name is the name of repeated title in the array1count is the number of repeated object by titlevalues由標題存儲所有重複對象

所以過濾器後,我想這樣的

array1=[ 
    {title: 'yes', time: "00:01:50"}, 
    {title: 'no', time: "00:02:50"}, 
    ]; 

array2 = [ 
    { 
     title :'yes', 
     count:4, 
     values:[ 
      {title: 'yes', time: "00:02:50"}, 
      {title: 'yes', time: "00:01:50"}, 
      {title: 'yes', time: "02:01:50"}, 
     ] 
    }, 
    { 
     title :'no', 
     count:2, 
     values:[ 
      {title: 'no', time: "10:01:50"} 
     ]; 
    } 
]; 

詳細說明我建立一個網站在2角像toggl timer如u可以在這裏看到在PIC My website所以當結果我點擊播放按鈕,暫停與輸入值title和時間timer value被添加到陣列1和這裏另一個目的是我angular2代碼

startPlay(name:string = "", time:string = "00:00:00") { 
    this.taskInputvalue.next(''); 
    this.play.next(true); 
    this.pause.next(false); 
    this.timer.next("00:00:00"); 
    clearInterval(this.countup); 
    //push the task to array 
    if(name == "") { 
    name = "Add description"; 
    this.tasks.push({title: name, time: time}); 
    } else { 
    name = name; 
    this.tasks.push({title: name, time: time}); 
    } 
} 

但是當我重複任務的名稱,如PIC的結果顯示如下彼此,所以我想要什麼,如果鍵入task 1首次它顯示爲一個li但是當重複一遍,應該存放在另一個數組所以可以遍歷它並顯示exsisting李下面的值時,我點擊它,請訪問toggl timer 明白我的意思

+1

請包括嘗試解決方案,爲什麼他們沒有工作,以及預期的結果。這真的可以幫助我們找出你的代碼的問題。謝謝! –

+1

雖然可行,但這完全沒有必要,只是使代碼變得難以理解,而且@pala畲說。 – Dellirium

+0

@palaѕн我更新的問題,請檢查出來 –

回答

1

你需要解釋你需要更好的話,我還可以幫助你,如果這不是你想要的,因爲我不知道你想要什麼,我會寫我的理解,你需要的,如果這不是案件,評論,我們會解決它,但在未來的帖子中嘗試更清楚。

var input = [ 
    {title: 'yes', time: "00:01:50"}, 
    {title: 'yes', time: "00:02:50"}, 
    {title: 'yes', time: "00:01:50"}, 
    {title: 'no', time: "02:01:00"}, 
    {title: 'yes', time: "02:01:50"}, 
    {title: 'no', time: "10:01:50"}, 
]; 

function getData(arr) { 
    var results = {}; 
    var resultArr = []; 
    arr.forEach(function(el) { 
     if (results[el.title]){ 
     results[el.title].count++; 
     results[el.title].values.push(el); 
    }else { 
     results[el.title] = { 
     title: el.title, 
     count: 1, 
     values: [el] 
     }; 
    } 
    }) 
    for (o in results){ 
    resultArr.push(results[o]); 
    } 
    return resultArr; 
} 

var output = getData(input); 
+0

謝謝,這就是我正在尋找讚賞。 –

0

如果你的數組1和數組2是objetcs你可以試試這個解決方案: 比方說,你的陣列1現更名爲array和array2現在是newArray1和newArray2

newArray1: YourArray1ObjectTypo = []; 
newArray2: YourArray2ObjectTypo = []; 

// function to add the array 
insertInArray2() { 
    for (a1 in array) { 
    if (a1.title == 'yes') { 
     newArray1.push(a1); 
    } 
    else { 
     newArray2.push(a1); 
    } 
    } 
} 

Edit-01:Use glo在你的課堂上使用bal數組,所以你不需要返回任何東西。

+0

不,我不知道該值的標題所以這將是一個變量,請參閱我的問題上面,什麼是我想要 –

+0

你的問題實在是混淆的公式。 –

+0

請訪問toggl。並查看當您重複任務名稱兩次而不管時間和結果與我的圖片進行比較時發生的情況,謝謝感謝您的耐心等待 –