按角度控制器中的顺序和最后一个数组

时间:2017-10-12 17:23:37

标签: angularjs model-view-controller

我的阵列看起来像这样

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 by这样的东西,并根据angularcontroller.js中的qid输入最后一行。

我尝试使用reduce功能,但它并没有给我我想要的内容

自从我开始使用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;
        });
    });
});

我得到的第一个项目不是后续的

2 个答案:

答案 0 :(得分: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;
           }
    }
    })

请检查一下是否有效。

注意:我不确定您是否需要对阵列进行排序。如果它已按要求的顺序排列,则无需排序,您可以直接运行forEach。否则,您必须编写一个sort函数,以便为forEach

提供所需的数组

答案 1 :(得分:1)

您可以使用reducemap的组合来解决此问题。

首先使用reduceqid分组数组:

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

然后使用具有最高ID的元素映射此组的值。您可以在此地图中使用其他ID找到具有最高reduce的元素:

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