skip to Main Content

I’m trying to compare this array of object :

"platforms": [
        {
            "id": 1,
            "name": "KF",
            "bankAccounts": [
                {
                    "id": 22,
                    "balance": -100,
                    "lendingPlatformId": 3
                },
                {
                    "id": 27,
                    "balance": 500,
                    "lendingPlatformId": 4
                }
            ],
        },
        {
            "id": 3,
            "name": "CC",
            "bankAccounts": [
                {
                    "id": 23,
                    "balance": 100,
                    "lendingPlatformId": 1
                }
            ],
        },
        {
            "id": 4,
            "name": "DD",
            "bankAccounts": [
                {
                    "id": 28,
                    "balance": 0,
                    "lendingPlatformId": 1
                }
            ],
        }
    ]

I want to compare the platform[].id and match bankAccounts[].lendingPlatformId

for example:

bankAccounts[].id = 22, its lendingPlatformId = 3, so it need to find platform[].id = 3 and bankAccounts[].id = 23 and lendingPlatformId = 1 ,then compare their balance’s sum is equal to zero, than push to new array.

enter image description here

expecting result is one new array:

isEqualToZero = [true, false, true, false]

(order is matter)

I’m thinking make it new object like:

platofmrId = 1 :{ lendingPlatformId: 3, balance:100 }, {lendingPlatformId: 4, balance:500 }

platofmrId = 3 :{ lendingPlatformId: 1, balance:-100 }

but seems it can’t achieve what i want

i’ve no idea how to do it…

please help, Thanks!

3

Answers


  1. const res=[] //to save result
    platforms.forEach(platform=>{ //go through platforms
    platform.bankAccounts.forEach(bank=>{ //go through platform bank account
        // to get lendingPlatform
        const lendPlatform=platforms.find(p=>p.id==bank.lendingPlatformId);
        //add the balance and compare 
        if((lendPlatform.bankAccounts[0].balance+bank.balance)==0)
            res.push(true) // if combined balance is zero
        else
            res.push(false) 
    })})
    console.log(res)
    
    Login or Signup to reply.
  2. const platforms = [
      {
          "id": 1,
          "name": "KF",
          "bankAccounts": [
              {
                  "id": 22,
                  "balance": -100,
                  "lendingPlatformId": 3
              },
              {
                  "id": 27,
                  "balance": 500,
                  "lendingPlatformId": 4
              }
          ]
      },
      {
          "id": 3,
          "name": "CC",
          "bankAccounts": [
              {
                  "id": 23,
                  "balance": 100,
                  "lendingPlatformId": 1
              }
          ]
      },
      {
          "id": 4,
          "name": "DD",
          "bankAccounts": [
              {
                  "id": 28,
                  "balance": 0,
                  "lendingPlatformId": 1
              }
          ]
      }
    ];
    const emptyArrayInit = Array.from(new Array(4), ()=>[0,0,0,0])
    platforms.forEach(platform=>{
      const {id, bankAccounts}=platform;
      const index = id-1;
      bankAccounts.forEach(bankAccount=>{
        const {balance,lendingPlatformId  } =bankAccount;
        const lendingPlatformIdIndex = lendingPlatformId-1;
        if(balance>0){
          emptyArrayInit[index][lendingPlatformIdIndex] += balance;
        }else{
          emptyArrayInit[lendingPlatformIdIndex][index] += balance
        }
      })
    })
    console.log(emptyArrayInit,'emptyArrayInit');
    

    /// [ [ 0, 0, 0, 500 ], [ 0, 0, 0, 0 ], [ 0, 0, 0, 0 ], [ 0, 0, 0, 0 ] ]

    it’s simple to reach your goal.

    Login or Signup to reply.
  3.     const res=[] //to save result
    platforms.map(platform=>{ //first loop
    platform.bankAccounts.map(bank=>{ // inner loop bankaccounts
        // to get lendingPlatforms
        const lendPlatforms=platforms.find(p=>p.id==bank.lendingPlatformId);
        //compare balance
        if((lendPlatforms.bankAccounts[0].balance+bank.balance)==0)
            res.push(true) // if combined balance is equal to zero
        else
            res.push(false) 
    })})
    console.log(res)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search