2017-10-12 23 views
0

我的數組類似陣列組按順序通過,並最後在角控制器

var List = [ 
    {qid: 1, ID: 1, text: "XXX",...}, 
    {qid: 1, ID: 2, text: "XXX",...}, 
    {qid: 1, ID: 3, text: "XXX",...}, 
    {qid: 2, ID: 4, text: "XXX",...}, 
    {qid: 2, ID: 5, text: "XXX",...}, 
    {qid: 3, ID: 6, text: "XXX",...}, 
    {qid: 3, ID: 7, text: "XXX",...}, 
    {qid: 3, ID: 8, text: "XXX",...}, 
]; 

我要查詢的數組,這樣最終名單看起來像這樣

var FinalList = [ 
    {qid: 1, ID: 3, text: "XXX",...}, 
    {qid: 2, ID: 5, text: "XXX",...}, 
    {qid: 3, ID: 8, text: "XXX",...}, 
]; 

QID可以有多個ID,但最後一個入口將是爲FinalList []數組選擇的一個。

我想在qid上使用像group一樣的東西,並根據angularcontroller.js中的qid獲取最後一行。

我試圖用減少功能,但它不給我我想要什麼

其短短一個月以來我一直在使用angularjs任何幫助將開始大爲讚賞

增加:

我試着這樣做

angular.forEach($scope.selectedList, function (item) {     

    var found = $filter('filter')($scope.selectedList, { QuesID: item.qid}); 
    $scope.FinalList .push(found[found.length - 1]); 

    $scope.List = $scope.List .filter(function (o1) { 
     return !$scope.List.some(function (o2) { 
      return o1.qid=== o2.qid; 
     }); 
    }); 
}); 

我得到的第一個項目沒有後續

回答

1

您可以使用reducemap的組合來解決此問題。

首先使用reduce,以便將陣列通過qid

var grouped = List.reduce(function(agg, x){ 
    (agg[x['qid']] = agg[x['qid']] || []).push(x); 
    return agg; 
}, {}); 

然後映射該組的值,與具有最高ID的元素。您可以使用另一個reduce此地圖內找到具有最高ID元素:

var result = Object.values(grouped).map(function(grp){ 
    return grp.reduce(function(a, b){ 
     return a['ID'] > b['ID'] ? a : b; 
    }); 
}); 

這是我認爲最乾淨的解決方案。

Here is a Plunker showing it in action

1

它的Javascript問題比angularJS更多。 好吧,這裏有雲:

排序數組第一:(學分:https://stackoverflow.com/a/8837511/6347317

var sortedList = List.sort(function(a, b){ 
    var keyA = a.qid;  
    keyB = b.qid;   
if(keyA < keyB) return -1; 
if(keyA > keyB) return 1; 
return 0; 
}); 

分配所需的變量:

var firstqid=sortedList[0].qid; //first object 
var finObjArr = []; //final array 
var finObj={}; //empty object to keep track of the object to be inserted in final array 
var lastObj = sortedList.slice(-1)[0]; //last object of sorted array 
var flag = 0; //flag to not insert any more object to final result if the last unique qid is reached 

遍歷數組並得到結果:(finObjArr會具有期望的輸出)

sortedList.forEach(function(obj) { 
     var qidkey = obj.qid; 
    //we are checking if current qid is same as the last one. this is to determine the last object with a qid key and then pushing the previous object (held in finObj) to the final array 
     if (qidkey != firstqid) 
    { 
      firstqid=qidkey; 
      finObjArr.push(finObj); 
    } 
//If the qid is same as earlier one, then make the current object as finObj and if its the last unique qid, inset the last object in the array in final array and set the flag so that no more inserts happen into final array 
    if (qidkey == firstqid) 
    {  
     finObj = obj; 
     if (qidkey == lastObj.qid && flag == 0) { 
      finObjArr.push(lastObj); 
      flag=1; 
      } 
    } 
    }) 

請c嘿,如果這個工程。我不確定你是否需要在數組上排序。如果它已經按照所需的順序進行,那麼不需要排序,您可以直接運行forEach。否則,您將不得不編寫排序功能,以使數組按照所需的順序排列forEach

+0

它過濾fqid = 1而不是QID 2,3,4 – DotNetBeginner

+0

我與你給同一陣列測試,它在工作。你可以給你使用的陣列嗎? Plunker:http://plnkr.co/edit/Y6M8X2DUXYfa41NNGCtn?p=preview –

+0

感謝您的幫助Vijay。我正在看着@Nikolaj Dam Larsen的回答。但我已經提出你的答案有用。再次感謝 – DotNetBeginner