Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to compare value in 2 different array of object in javascript

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading