Airea
Airea

Reputation: 179

Adding a property inside an object that has been filtered from an array of objects

I have this array of objects ("clients") which I compare with another array called "contracts" that have some IDs that appear in "clients".

I have already filtered "clients" and I'm able to find which objects have the same id. However, I can't add a new property inside that object!

Here are examples of both arrays

CLIENTS:

const clients = [{"id":"a0ece5db-cd14-4f21-812f-966633e7be86","name":"Anna","email":"[email protected]","role":"user"},
{"id":"e8fd159b-57c4-4d36-9bd7-a59ca13057bb","name":"Jack","email":"[email protected]","role":"admin"},
{"id":"a3b8d425-2b60-4ad7-becc-bedf2ef860bd","name":"Barnett","email":"[email protected]","role":"user"},
{"id":"44e44268-dce8-4902-b662-1b34d2c10b8e","name":"Merrill","email":"[email protected]","role":"user"},
{"id":"0178914c-548b-4a4c-b918-47d6a391530c","name":"Whitley","email":"[email protected]","role":"admin"},
{"id":"a74c83c5-e271-4ecf-a429-d47af952cfd4","name":"Lamb","email":"[email protected]","role":"user"},
{"id":"55601290-8619-4f54-b831-9c6c26c52b44","name":"Ophelia","email":"[email protected]","role":"user"},
{"id":"1470c601-6833-48a4-85b4-ddab9c045916","name":"Jerry","email":"[email protected]","role":"user"}];

CONTRACTS:

const contracts = [{"id":"64cceef9-3a01-49ae-a23b-3761b604800b","amountInsured":"1825.89","inceptionDate":"2016-06-01T03:33:32Z","installmentPayment":true,"clientId":"e8fd159b-57c4-4d36-9bd7-a59ca13057bb"},
{"id":"7b624ed3-00d5-4c1b-9ab8-c265067ef58b","amountInsured":"399.89","inceptionDate":"2015-07-06T06:55:49Z","installmentPayment":true,"clientId":"a0ece5db-cd14-4f21-812f-966633e7be86"},
{"id":"56b415d6-53ee-4481-994f-4bffa47b5239","amountInsured":"2301.98", "inceptionDate":"2014-12-01T05:53:13Z","installmentPayment":false,"clientId":"e8fd159b-57c4-4d36-9bd7-a59ca13057bb"},
{"id":"6f514ec4-1726-4628-974d-20afe4da130c","amountInsured":"697.04", "inceptionDate":"2014-09-12T12:10:23Z","installmentPayment":false,"clientId":"a0ece5db-cd14-4f21-812f-966633e7be86"},
{"id":"25202f31-fff0-481c-acfd-1f3ff2a9bcbe","amountInsured":"2579.16", "inceptionDate":"2016-05-03T04:58:48Z","installmentPayment":false,"clientId":"a0ece5db-cd14-4f21-812f-966633e7be86"},
{"id":"15b4430d-96f8-468e-98c0-3caaf8b0b3b6","amountInsured":"645.65", "inceptionDate":"2016-01-15T02:56:48Z","installmentPayment":true,"clientId":"a0ece5db-cd14-4f21-812f-966633e7be86"},
{"id":"5a72ae47-d077-4f74-9166-56a6577e31b9","amountInsured":"751.67", "inceptionDate":"2015-08-05T04:05:01Z","installmentPayment":true,"clientId":"e8fd159b-57c4-4d36-9bd7-a59ca13057bb"},
{"id":"4a582500-fab6-4efe-ae89-0c9ed750d0c7","amountInsured":"3074.24", "inceptionDate":"2014-06-24T09:21:06Z","installmentPayment":false,"clientId":"a0ece5db-cd14-4f21-812f-966633e7be86"}]

The following code compares the id from the array "clients" with the "contracts" one.

FILTERING CODE:

clients.filter((e) => {
    return contracts.some((j) => {
      return e.id === j.clientId
    })
})

It works, it ends showing that two clients are the ones that have the contracts. I thought that changing the previous code to the next one would add as many porperties as contracts tha same client has:

clients.filter((e) => {
        return contracts.some((j) => {
            if (e.id === j.clientId) {
                clients.push(e[j.id] = {
                    "amountInsured": j.amountInsured,
                    "inceptionDate": j.inceptionDate,
                    "installmentPayment": j.installmentPayment
                })
            }
        })
})

But it doesn't work... As you can see, my idea is to add properties from objects from the array "contracts" that have the same id than "clients" so I can have the array "clients" updated with these properties. Because there can be many contracts with the same id (clientId), I need to add the property name ina dynamically way (I0ve chosen to use the id of the contract).

I would really appreciate any help, Thanks a lot!

Upvotes: 0

Views: 51

Answers (3)

user120242
user120242

Reputation: 15268

This is really inefficient if you have contracts and clients of large size. For small arrays it will perform fine, and may be slightly faster, because it avoids some object initialization and iterating over an integer indexed homogeneous array is fast.
But the code is very short and concise.
You can use an object map if the size of the arrays can be very large (so clientId lookups can be O(1)).

(Put all contracts under one object)

for (const client of clients) {
  const mycontracts = contracts.filter(c=>c.clientId===client.id)
  if(mycontracts.length)
    client.contracts = Object.fromEntries(
      mycontracts.map(({id, clientId, ...data}) => [id, data]))
}
console.log(
  clients
)
<script>
const clients = [{"id":"a0ece5db-cd14-4f21-812f-966633e7be86","name":"Anna","email":"[email protected]","role":"user"},
{"id":"e8fd159b-57c4-4d36-9bd7-a59ca13057bb","name":"Jack","email":"[email protected]","role":"admin"},
{"id":"a3b8d425-2b60-4ad7-becc-bedf2ef860bd","name":"Barnett","email":"[email protected]","role":"user"},
{"id":"44e44268-dce8-4902-b662-1b34d2c10b8e","name":"Merrill","email":"[email protected]","role":"user"},
{"id":"0178914c-548b-4a4c-b918-47d6a391530c","name":"Whitley","email":"[email protected]","role":"admin"},
{"id":"a74c83c5-e271-4ecf-a429-d47af952cfd4","name":"Lamb","email":"[email protected]","role":"user"},
{"id":"55601290-8619-4f54-b831-9c6c26c52b44","name":"Ophelia","email":"[email protected]","role":"user"},
{"id":"1470c601-6833-48a4-85b4-ddab9c045916","name":"Jerry","email":"[email protected]","role":"user"}];
const contracts = [{"id":"64cceef9-3a01-49ae-a23b-3761b604800b","amountInsured":"1825.89","inceptionDate":"2016-06-01T03:33:32Z","installmentPayment":true,"clientId":"e8fd159b-57c4-4d36-9bd7-a59ca13057bb"},
{"id":"7b624ed3-00d5-4c1b-9ab8-c265067ef58b","amountInsured":"399.89","inceptionDate":"2015-07-06T06:55:49Z","installmentPayment":true,"clientId":"a0ece5db-cd14-4f21-812f-966633e7be86"},
{"id":"56b415d6-53ee-4481-994f-4bffa47b5239","amountInsured":"2301.98", "inceptionDate":"2014-12-01T05:53:13Z","installmentPayment":false,"clientId":"e8fd159b-57c4-4d36-9bd7-a59ca13057bb"},
{"id":"6f514ec4-1726-4628-974d-20afe4da130c","amountInsured":"697.04", "inceptionDate":"2014-09-12T12:10:23Z","installmentPayment":false,"clientId":"a0ece5db-cd14-4f21-812f-966633e7be86"},
{"id":"25202f31-fff0-481c-acfd-1f3ff2a9bcbe","amountInsured":"2579.16", "inceptionDate":"2016-05-03T04:58:48Z","installmentPayment":false,"clientId":"a0ece5db-cd14-4f21-812f-966633e7be86"},
{"id":"15b4430d-96f8-468e-98c0-3caaf8b0b3b6","amountInsured":"645.65", "inceptionDate":"2016-01-15T02:56:48Z","installmentPayment":true,"clientId":"a0ece5db-cd14-4f21-812f-966633e7be86"},
{"id":"5a72ae47-d077-4f74-9166-56a6577e31b9","amountInsured":"751.67", "inceptionDate":"2015-08-05T04:05:01Z","installmentPayment":true,"clientId":"e8fd159b-57c4-4d36-9bd7-a59ca13057bb"},
{"id":"4a582500-fab6-4efe-ae89-0c9ed750d0c7","amountInsured":"3074.24", "inceptionDate":"2014-06-24T09:21:06Z","installmentPayment":false,"clientId":"a0ece5db-cd14-4f21-812f-966633e7be86"}]
</script>

Upvotes: 1

Kirmin
Kirmin

Reputation: 301

This should do the work:

for (let j in clients) {
    for (let i in contracts) {
        if(contracts[i].id==clients[j].id){
            clients[j].amountInsured = contracts[i].amountInsured;
            clients[j].inceptionDate = contracts[i].inceptionDate;        
            clients[j].installmentPayment = contracts[i].installmentPayment;
        }
    }
}

Upvotes: 0

osekmedia
osekmedia

Reputation: 683

Some should return boolean and because filter returns a new array, something like this would work:

let filteredClients = [];
filteredClients = clients.filter((e) => {
        return contracts.some((j) => {
            if (e.id === j.clientId) {
                e[j.id] = {
                    "amountInsured": j.amountInsured,
                    "inceptionDate": j.inceptionDate,
                    "installmentPayment": j.installmentPayment
                }
                return true;
            }
            return false;
        })
});

console.log(filteredClients); 

Upvotes: 0

Related Questions