2017-10-12 67 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