如何在JavaScript中的数组中对数组中的属性进行排序?

问题描述 投票:0回答:1

我有一个数组,它有多个汇率数组,我想对浮动的属性进行排序

这是我正在尝试

let newArrayRates = [];
newArrayRates = banksData;

newArrayRates.forEach(element => {
    element.rates.sort(function(a,b){

        element.rates.sort(function(a, b) {

            let textA = a; 
            let textB = b;

            return textA.floating <
                textB.floating
                ? -1
                : textA.floating >
                textB.floating
                ? 1 
                : 0;

    })

这是按属性浮动排序的数组

var banksData =[ 
   { 
      "name":"ANZ2",
      "rates":[ 
         { 
            "bankName":"ANZ",
            "productName":"ANZ",
            "floating":6.9,
            "sixMonths":4.79,
            "oneYear":4.15,
            "eighteenMonths":4.49,
            "twoYears":4.09,
            "threeYears":4.49,
            "fourYears":4.75,
            "fiveYears":4.85,
            "sixYears":0.0,
            "revolvingCredit":5.8,
            "isSpecial":false
         },
         { 
            "bankName":"ANZ Bank",
            "productName":"ANZ Special",
            "floating":2.0,
            "sixMonths":4.29,
            "oneYear":3.65,
            "eighteenMonths":3.99,
            "twoYears":3.59,
            "threeYears":3.99,
            "fourYears":0.0,
            "fiveYears":0.0,
            "sixYears":0.0,
            "revolvingCredit":0.0,
            "isSpecial":true
         }
      ],
      "knownDiscount":[ 

      ],
      "type":[ 
         1,
         4
      ],
      "mortgageFeatures":{ 
         "welcomeHomeLoans":"No",
         "kiwiBuild":"Yes",
         "preApproval":"Up to 90 days",
         "interestOnly":"Yes",
         "redraw":"No",
         "revolvingCredit":"Yes",
         "amortisingRevolvingCredit":"No",
         "revolvingCreditMonthlyFee":"$12.50",
         "offset":"No",
         "ownerOccupiedLoanTerm":"30 Years",
         "investmentPropertyLoanTerm":"30 Years",
         "earlyRepayment":"Yes"
      },
      "url":"/banks/ANZ2",
      "website":"https://www.anz.co.nz/personal/"
   },
   { 
      "name":"ASB ",
      "rates":[ 
         { 
            "bankName":"ASB1",
            "productName":"ASB",
            "floating":5.2,
            "sixMonths":4.79,
            "oneYear":4.15,
            "eighteenMonths":4.25,
            "twoYears":4.09,
            "threeYears":4.39,
            "fourYears":4.69,
            "fiveYears":4.79,
            "sixYears":0.0,
            "revolvingCredit":5.3,
            "isSpecial":false
         },
         { 
            "bankName":"ASB Bank",
            "productName":"ASB Special",
            "floating":5.0,
            "sixMonths":4.29,
            "oneYear":3.75,
            "eighteenMonths":3.75,
            "twoYears":3.59,
            "threeYears":3.89,
            "fourYears":4.19,
            "fiveYears":4.29,
            "sixYears":0.0,
            "revolvingCredit":0.0,
            "isSpecial":true
         }
      ],
      "knownDiscount":[ 

      ],
      "type":[ 
         1,
         4
      ],
      "mortgageFeatures":{ 
         "welcomeHomeLoans":"No",
         "kiwiBuild":"Yes",
         "preApproval":"Up to 90 days",
         "interestOnly":"Yes",
         "redraw":"No",
         "revolvingCredit":"Yes",
         "amortisingRevolvingCredit":"Yes",
         "revolvingCreditMonthlyFee":"$12.00",
         "offset":"No",
         "ownerOccupiedLoanTerm":"30 years",
         "investmentPropertyLoanTerm":"25 years",
         "earlyRepayment":"Yes"
      },
      "url":"/banks/ASB ",
      "website":"https://www.asb.co.nz/"
   },
   { 
      "name":"BNZ",
      "rates":[ 
         { 
            "bankName":"BNZ",
            "productName":"BNZ Owner Occupied Standard",
            "floating":5.3,
            "sixMonths":4.79,
            "oneYear":4.45,
            "eighteenMonths":4.55,
            "twoYears":4.35,
            "threeYears":4.55,
            "fourYears":4.95,
            "fiveYears":5.15,
            "sixYears":0.0,
            "revolvingCredit":0.0,
            "isSpecial":false
         },
         { 
            "bankName":"BNZ",
            "productName":"BNZ Owner Occupied Special",
            "floating":0.0,
            "sixMonths":0.0,
            "oneYear":3.65,
            "eighteenMonths":0.0,
            "twoYears":3.59,
            "threeYears":3.99,
            "fourYears":4.35,
            "fiveYears":4.45,
            "sixYears":0.0,
            "revolvingCredit":0.0,
            "isSpecial":true
         },
         { 
            "bankName":"BNZ",
            "productName":"BNZ Investor Standard",
            "floating":5.55,
            "sixMonths":5.04,
            "oneYear":4.7,
            "eighteenMonths":4.8,
            "twoYears":4.6,
            "threeYears":4.8,
            "fourYears":5.2,
            "fiveYears":5.4,
            "sixYears":0.0,
            "revolvingCredit":0.0,
            "isSpecial":false
         },
         { 
            "bankName":"BNZ",
            "productName":"BNZ Investor Special",
            "floating":5.55,
            "sixMonths":6.3,
            "oneYear":3.9,
            "eighteenMonths":0.0,
            "twoYears":3.84,
            "threeYears":4.24,
            "fourYears":4.6,
            "fiveYears":4.7,
            "sixYears":0.0,
            "revolvingCredit":0.0,
            "isSpecial":false
         }
      ],
      "knownDiscount":[ 

      ],
      "type":[ 
         1,
         4
      ],
      "mortgageFeatures":{ 
         "welcomeHomeLoans":"No",
         "kiwiBuild":"Yes",
         "preApproval":"Up to 90 days",
         "interestOnly":"Yes",
         "redraw":"Yes",
         "revolvingCredit":"No",
         "amortisingRevolvingCredit":"Yes",
         "revolvingCreditMonthlyFee":"$2",
         "offset":"Yes",
         "ownerOccupiedLoanTerm":"30 years",
         "investmentPropertyLoanTerm":"30 years",
         "earlyRepayment":"Yes"
      },
      "url":"/banks/BNZ",
      "website":"https://www.bnz.co.nz/"
   },

我尝试了很多次,但它只是对每个数组的速率进行排序,而不对所有浮点数进行排序。我想对数组速率的浮动属性进行排序

javascript
1个回答
0
投票

问题不是很清楚。这是您想要的吗?

const sortRatesByFloating = banks => banks .map (({rates, ...rest}) => ({
  ...rest, 
  rates: rates .slice(0) .sort ((a, b, x = a .floating, y = b .floating) => x - y)
}))


const banksData=[{name:"ANZ2",rates:[{bankName:"ANZ",productName:"ANZ",floating:6.9,sixMonths:4.79,oneYear:4.15,eighteenMonths:4.49,twoYears:4.09,threeYears:4.49,fourYears:4.75,fiveYears:4.85,sixYears:0,revolvingCredit:5.8,isSpecial:!1},{bankName:"ANZ Bank",productName:"ANZ Special",floating:2,sixMonths:4.29,oneYear:3.65,eighteenMonths:3.99,twoYears:3.59,threeYears:3.99,fourYears:0,fiveYears:0,sixYears:0,revolvingCredit:0,isSpecial:!0}],knownDiscount:[],type:[1,4],mortgageFeatures:{welcomeHomeLoans:"No",kiwiBuild:"Yes",preApproval:"Up to 90 days",interestOnly:"Yes",redraw:"No",revolvingCredit:"Yes",amortisingRevolvingCredit:"No",revolvingCreditMonthlyFee:"$12.50",offset:"No",ownerOccupiedLoanTerm:"30 Years",investmentPropertyLoanTerm:"30 Years",earlyRepayment:"Yes"},url:"/banks/ANZ2",website:"https://www.anz.co.nz/personal/"},{name:"ASB ",rates:[{bankName:"ASB1",productName:"ASB",floating:5.2,sixMonths:4.79,oneYear:4.15,eighteenMonths:4.25,twoYears:4.09,threeYears:4.39,fourYears:4.69,fiveYears:4.79,sixYears:0,revolvingCredit:5.3,isSpecial:!1},{bankName:"ASB Bank",productName:"ASB Special",floating:5,sixMonths:4.29,oneYear:3.75,eighteenMonths:3.75,twoYears:3.59,threeYears:3.89,fourYears:4.19,fiveYears:4.29,sixYears:0,revolvingCredit:0,isSpecial:!0}],knownDiscount:[],type:[1,4],mortgageFeatures:{welcomeHomeLoans:"No",kiwiBuild:"Yes",preApproval:"Up to 90 days",interestOnly:"Yes",redraw:"No",revolvingCredit:"Yes",amortisingRevolvingCredit:"Yes",revolvingCreditMonthlyFee:"$12.00",offset:"No",ownerOccupiedLoanTerm:"30 years",investmentPropertyLoanTerm:"25 years",earlyRepayment:"Yes"},url:"/banks/ASB ",website:"https://www.asb.co.nz/"},{name:"BNZ",rates:[{bankName:"BNZ",productName:"BNZ Owner Occupied Standard",floating:5.3,sixMonths:4.79,oneYear:4.45,eighteenMonths:4.55,twoYears:4.35,threeYears:4.55,fourYears:4.95,fiveYears:5.15,sixYears:0,revolvingCredit:0,isSpecial:!1},{bankName:"BNZ",productName:"BNZ Owner Occupied Special",floating:0,sixMonths:0,oneYear:3.65,eighteenMonths:0,twoYears:3.59,threeYears:3.99,fourYears:4.35,fiveYears:4.45,sixYears:0,revolvingCredit:0,isSpecial:!0},{bankName:"BNZ",productName:"BNZ Investor Standard",floating:5.55,sixMonths:5.04,oneYear:4.7,eighteenMonths:4.8,twoYears:4.6,threeYears:4.8,fourYears:5.2,fiveYears:5.4,sixYears:0,revolvingCredit:0,isSpecial:!1},{bankName:"BNZ",productName:"BNZ Investor Special",floating:5.55,sixMonths:6.3,oneYear:3.9,eighteenMonths:0,twoYears:3.84,threeYears:4.24,fourYears:4.6,fiveYears:4.7,sixYears:0,revolvingCredit:0,isSpecial:!1}],knownDiscount:[],type:[1,4],mortgageFeatures:{welcomeHomeLoans:"No",kiwiBuild:"Yes",preApproval:"Up to 90 days",interestOnly:"Yes",redraw:"Yes",revolvingCredit:"No",amortisingRevolvingCredit:"Yes",revolvingCreditMonthlyFee:"$2",offset:"Yes",ownerOccupiedLoanTerm:"30 years",investmentPropertyLoanTerm:"30 years",earlyRepayment:"Yes"},url:"/banks/BNZ",website:"https://www.bnz.co.nz/"}];

console .log (sortRatesByFloating (banksData))

在此功能中,每个银行的汇率均按其floating属性进行排序(升序)。我不修改原始结构,而是返回带有一个新的排序速率数组的浅表副本。

还是您想要其他东西?

© www.soinside.com 2019 - 2024. All rights reserved.