基于第二个嵌套数组对嵌套数组进行排序

时间:2018-03-14 12:54:39

标签: javascript arrays angularjs

根据David Lewis在Javascript - sort array based on another array中的回答,我试图根据名为clientRef的属性的第二个嵌套数组的顺序对表数据进行排序。

vm.tableData = transactions; //populated with data from response stored in variable (original data)

var clientRefArr = [];

vm.tableData.forEach(function(data) {
    var res = data.renewalUIs.map(function(o){ 
        clientRefArr.push(o.patentUI.clientRef) //The data in which I need to sort the table with
    })
})

clientRefArr.sort(); //sort array

var aSorted = [], bSorted = []; //need arrays of the clientRef

function sortFunc(a, b) {

    a.renewalUIs.forEach(function(data, i, index){
        aSorted.push(data.patentUI.clientRef);
    })

    b.renewalUIs.forEach(function(data, i){
        bSorted.push(data.patentUI.clientRef);
    })

    return clientRefArr.indexOf(aSorted[1]) - clientRefArr.indexOf(bSorted[1]); //following the answer from the linked stackoverflow, I compare the two arrays

}

vm.tableData.sort(sortFunc)

我需要根据clientRef数组的顺序对vm.tableData的outter数组进行排序,其中一些数组的值为null。到目前为止我没有成功,也不确定如何处理这个问题。

问题

如何根据vm.tableData数组的顺序对clientRef进行排序?

**clientRef array** (order array)

[
 0:""
 1:""
 2:"CWORD"
 3:"DWORD"
 4:"FWORD"
 5:"GWORD"
 6:"HWORD"
]

**vm.tableData** (data to be sorted)

[
 {
   status: 'awaiting funds'
   transRef: 'TRAN000192'
   renewalUIs: [
    {
     patent: null
     fee: 1500
     patentUI: {
      patentNo: EP27237
      bandColour: green
      clientRef: FWORD //here is the nested clientRef which I then sort in the clientRefArr
     }
    }
   ]
 }
]

0 个答案:

没有答案
相关问题