我有
obj1= [{keyID: 'c71cc89335565241f59ac9bb9da162a3ddf4a6b483bdc06dd82b9275898ba5ee', name: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="324741574003725f535b5e1c515d5f" rel="noreferrer noopener nofollow">[email protected]</a>'},
{keyID: '34051d2554ab20b0d24c2c39946d96da7e07508e7a8546bc2df299601dcbff05', name: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2c595f495e1e6c414d4540024f4341" rel="noreferrer noopener nofollow">[email protected]</a>'}]
还有
obj2=[{id: 19, email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="126761776026527f737b7e3c717d7f" rel="noreferrer noopener nofollow">[email protected]</a>', other_field: false},
{id: 221, email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="413432243372012c20282d6f222e2c" rel="noreferrer noopener nofollow">[email protected]</a>', other_field: false},
{id: 13, email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d0a5a3b5a2e290bdb1b9bcfeb3bfbd" rel="noreferrer noopener nofollow">[email protected]</a>', other_field: false)},
{id: 2, email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b2e283e296a1b363a323775383436" rel="noreferrer noopener nofollow">[email protected]</a>', other_field: true]
如您所见,我的 obj1 keyID
与 obj2 ID
不同,这很好。我想要将它们分组的依据是名称(obj1)和电子邮件(obj2)
具有相同电子邮件地址的字段应创建一个具有相同结构 obj2 的新对象,并带有新字段“isPresent
”
result=[{id: 19, email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6b1e180e195f2b060a020745080406" rel="noreferrer noopener nofollow">[email protected]</a>', other_field: false,isPresent=false},
{id: 221, email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8df8fee8ffbecde0ece4e1a3eee2e0" rel="noreferrer noopener nofollow">[email protected]</a>', other_field: false,isPresent=false},
{id: 13, email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1560667067275578747c793b767a78" rel="noreferrer noopener nofollow">[email protected]</a>', other_field: false,isPresent=true)},
{id: 2, email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0d787e687f3c4d606c6461236e6260" rel="noreferrer noopener nofollow">[email protected]</a>', other_field: true,isPresent=true]
我怎样才能实现这一目标?
最佳答案
您可以map通过 obj2
并通过 some 检查 obj1
中是否存在同名元素.
我也spread map 内的 obj
而不是直接分配 isPresent
以避免副作用。
const obj1 = [{ keyID: 'c71cc89335565241f59ac9bb9da162a3ddf4a6b483bdc06dd82b9275898ba5ee', name: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4b1b7a1b6f584a9a5ada8eaa7aba9" rel="noreferrer noopener nofollow">[email protected]</a>' }, { keyID: '34051d2554ab20b0d24c2c39946d96da7e07508e7a8546bc2df299601dcbff05', name: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82f7f1e7f0b0c2efe3ebeeace1edef" rel="noreferrer noopener nofollow">[email protected]</a>' }];
const obj2 = [{ id: 19, email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ef9a9c8a9ddbaf828e8683c18c8082" rel="noreferrer noopener nofollow">[email protected]</a>', other_field: false }, { id: 221, email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="374244524504775a565e5b1954585a" rel="noreferrer noopener nofollow">[email protected]</a>', other_field: false }, { id: 13, email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f386809681c1b39e929a9fdd909c9e" rel="noreferrer noopener nofollow">[email protected]</a>', other_field: false }, { id: 2, email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="532620362162133e323a3f7d303c3e" rel="noreferrer noopener nofollow">[email protected]</a>', other_field: true }];
const result = obj2.map((obj) => ({
...obj,
isPresent: obj1.some(({ name }) => name === obj.email),
}));
console.log(result);
关于javascript - 如何比较具有不同字段的 2 个不同对象,并使用告诉共同元素的新字段创建一个新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69662938/