如何检查数组中的值,如果存在,则在另一个数组中添加内容?
我有两个非常大的数组,一个是人的数组,另一个是博客的数组。我需要使用js来实现这一点。我的数组看起来像这样(我在示例中缩短了数组):
arr2 =
[
{
"post_title": "In-House Counsel’s Role in Optimizing Intellectual Property",
"post_date": "@1667970000",
"custom":
{
"author":
[
{
"type": "attorney",
"identifier": "Deborah Stehr"
}
]
}
},
{
"post_title": "Understanding the Privacy Right to Be Forgotten",
"post_date": "@1667797200",
"custom":
{
"author":
[
{
"type": "attorney",
"identifier": "Lori S. Ross"
}
]
}
},
{
"post_title": "3 Creative Ideas for Writing a More Compelling Collection Letter",
"post_date": "@1667361600",
"custom":
{
"author":
[
{
"type": "attorney",
"identifier": "Brian Heller"
}
]
}
},
{
"post_title": "FTC Shines a Light on Digital Dark Patterns",
"post_date": "@1666670400",
"custom":
{
"author":
[
{
"type": "attorney",
"identifier": "Lori S. Ross"
}
]
}
},
{
"post_title": "New California Law Requires Physicians to Notify Patients about Open Payments Database",
"post_date": "@1666584000",
"custom":
{
"author":
[
{
"type": "attorney",
"identifier": "Deborah Stehrn"
}
]
}
},
{
"post_title": "Exclusive Use Provisions in Commercial Leases",
"post_date": "@1663819200",
"custom":
{
"author":
[
{
"type": "attorney",
"identifier": "Brian Heller"
}
]
}
}
]
另一个数组
arr1 =
[
{
"post_title": "Deborah Stehr",
"custom":
{
"title": "Senior Counsel"
}
},
{
"post_title": "Lori S. Ross",
"custom":
{
"title": "Attorney, Member of the Firm"
}
},
{
"post_title": "Brian Heller",
"custom":
{
"title": "Attorney, Member of the Firm"
}
}
]
我希望arr1最后输出这个
[
{
"post_title": "Deborah Stehr",
"custom":
{
"title": "Senior Counsel",
"related_posts":
[
{
"type": "blog",
"identifier": "In-House Counsel’s Role in Optimizing Intellectual Property"
},
{
"type": "blog",
"identifier": "New California Law Requires Physicians to Notify Patients about Open Payments Database"
}
]
}
},
{
"post_title": "Lori S. Ross",
"custom":
{
"title": "Attorney, Member of the Firm",
"related_posts":
[
{
"type": "blog",
"identifier": "FTC Shines a Light on Digital Dark Patterns"
},
{
"type": "blog",
"identifier": "Understanding the Privacy Right to Be Forgotten"
}
]
}
},
{
"post_title": "Brian Heller",
"custom":
{
"title": "Attorney, Member of the Firm",
etc.
}
}
]
这就是我所尝试的。我打算每次为每个作者做一个新的循环。
for(var i=0, len = arr2.length; i < len; i++){
if(arr2[i].custom.author[0].identifier == 'Deborah Stehr'){
for(var t=0, len = arr1.length; t< len; t++){
if(arr1[t].post_title == 'Deborah Stehr'){
arr1[t].custom.related_posts = [];
arr1[t].custom.related_posts.push({
"type": "blog",
"identifier": arr2[i].post_title
})
}
}
}
}
for(var i=0, len = arr2.length; i < len; i++){
if(arr2[i].custom.author[0].identifier == 'Lori S. Ross'){
for(var t=0, len = arr1.length; t< len; t++){
if(arr1[t].post_title == 'Lori S. Ross'){
arr1[t].custom.related_posts = [];
arr1[t].custom.related_posts.push({
"type": "blog",
"identifier": arr2[i].post_title
})
}
}
}
}
这是可行的,但是它没有从arr2中得到每个对象。它只得到第一个对象。我如何循环通过两个数组来实现这一点?
1条答案
按热度按时间68bkxrlz1#
你能得到第一个是因为你知道
在添加相关帖子之前。您应该在查找所有相关帖子的循环之前执行一次此操作。
你不需要为每个帖子标题单独循环,只需循环
arr1
,然后在arr2
中找到所有匹配的元素。使用
forEach()
将使代码更容易阅读,因为您不需要所有那些带有大量数组索引的长名称。