我正在寻找一种搜索json文件并在比赛中合并json对象的解决方案。例如,我有两个json:
JSON 1
{
"guests": [
{
"customerID": 1,
"FirstName": "Max",
"LastName": "Miller",
"Email": "max123@gmail.com",
"CreditCard": [
{
"Expiration": "12/2020",
"FirstDigits": 0000,
"LastDigits": 0000
}
]
},
{
"customerID": 2,
"FirstName": "Luke",
"LastName": "Fetcher",
"Email": "lf123@gmail.com",
"CreditCard": [
{
"Expiration": "04/2020",
"FirstDigits": 1111,
"LastDigits": 1111
}
]
}]
}
JSON 2
{
"buyer": [
{
"FirstName": "Max",
"LastName": "Miller",
"Email": "max123@gmail.com",
"address": "Max Miller, Test street 1, NY",
"products": [
{
"productName": "Test",
"productPrice": 120,
"Size": 43
"Color": "red"
},
{
"productName": "Test 2",
"productPrice": 80,
"Size": 43
"Color": "yellow"
},
]
},
{
"customerID": 2,
"FirstName": "Luke",
"LastName": "Fetcher",
"Email": "lf123@gmail.com",
"address": "Luke Fetcher, 1st Road, San Fransisco"
}]
}
我的问题是:我如何通过电子邮件搜索和比较JSON 1和JSON 2,因为两者都具有电子邮件属性,如果它们匹配,则将JSON 2中缺少的值添加到JSON 1中。在这种情况下,JSON 2提供了产品列表和地址(JSON 1中缺少),并且当电子邮件匹配时,也应将产品添加到JSON 1中。我相信以某种方式使用JavaScript应该有可能,但是我不知道该怎么做。有人有主意吗?