简体   繁体   English

在已从对象数组中过滤的 object 中添加属性

[英]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".我有这个对象数组(“客户”),我将其与另一个名为“合同”的数组进行比较,该数组有一些出现在“客户”中的 ID。

I have already filtered "clients" and I'm able to find which objects have the same id.我已经过滤了“客户”,并且能够找到哪些对象具有相同的 ID。 However, I can't add a new property inside that object!但是,我不能在该对象内添加新属性!

Here are examples of both arrays以下是 arrays 的示例

CLIENTS:客户:

const clients = [{"id":"a0ece5db-cd14-4f21-812f-966633e7be86","name":"Anna","email":"annap@quotet.com","role":"user"},
{"id":"e8fd159b-57c4-4d36-9bd7-a59ca13057bb","name":"Jack","email":"jackp@quotet.com","role":"admin"},
{"id":"a3b8d425-2b60-4ad7-becc-bedf2ef860bd","name":"Barnett","email":"barnettp@quotet.com","role":"user"},
{"id":"44e44268-dce8-4902-b662-1b34d2c10b8e","name":"Merrill","email":"merrillp@quotet.com","role":"user"},
{"id":"0178914c-548b-4a4c-b918-47d6a391530c","name":"Whitley","email":"whitleyp@quotet.com","role":"admin"},
{"id":"a74c83c5-e271-4ecf-a429-d47af952cfd4","name":"Lamb","email":"lambbp@quotet.com","role":"user"},
{"id":"55601290-8619-4f54-b831-9c6c26c52b44","name":"Ophelia","email":"opheliap@quotet.com","role":"user"},
{"id":"1470c601-6833-48a4-85b4-ddab9c045916","name":"Jerry","email":"jerryp@quotet.com","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.以下代码将数组“clients”中的 id 与“contracts”进行比较。

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 .但它不起作用......如您所见,我的想法是从数组“contracts”中添加与“clients”具有相同ID的对象的属性,这样我就可以使用这些属性更新数组“clients” . 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).因为可以有许多具有相同 id (clientId) 的合约,所以我需要以动态方式添加属性名称(我已经选择使用合约的 id)。

I would really appreciate any help, Thanks a lot!我真的很感激任何帮助,非常感谢!

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.对于小的 arrays 它将执行得很好,并且可能会稍微快一些,因为它避免了一些 object 初始化和迭代 integer 索引同构数组很快。
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)).如果 arrays 的大小可能非常大(因此 clientId 查找可以是 O(1)),您可以使用 object map。

(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":"annap@quotet.com","role":"user"}, {"id":"e8fd159b-57c4-4d36-9bd7-a59ca13057bb","name":"Jack","email":"jackp@quotet.com","role":"admin"}, {"id":"a3b8d425-2b60-4ad7-becc-bedf2ef860bd","name":"Barnett","email":"barnettp@quotet.com","role":"user"}, {"id":"44e44268-dce8-4902-b662-1b34d2c10b8e","name":"Merrill","email":"merrillp@quotet.com","role":"user"}, {"id":"0178914c-548b-4a4c-b918-47d6a391530c","name":"Whitley","email":"whitleyp@quotet.com","role":"admin"}, {"id":"a74c83c5-e271-4ecf-a429-d47af952cfd4","name":"Lamb","email":"lambbp@quotet.com","role":"user"}, {"id":"55601290-8619-4f54-b831-9c6c26c52b44","name":"Ophelia","email":"opheliap@quotet.com","role":"user"}, {"id":"1470c601-6833-48a4-85b4-ddab9c045916","name":"Jerry","email":"jerryp@quotet.com","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>

Some should return boolean and because filter returns a new array, something like this would work:有些应该返回 boolean 并且因为 filter 返回一个新数组,所以这样的事情会起作用:

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); 

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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM