我下面有两个JSON数组对象。我想得到stdcontinfo JSON值的匹配stdname并将其添加到stdJson各自的JSON中。
有人可以帮我解决这个问题吗?
var stdJson = [{
"_id": ObjectId("5923ed1f610be38692e88e4d"),
"stdname": "Bala",
"fathername": "Kannappan",
"mark1": 78.0,
"mark2": 70.0,
"mark3": 98.0,
"mark4": 88.0,
"mark5": 100.0
},
{
"_id": ObjectId("5923ed99610be38692e88e4e"),
"stdname": "vetri",
"fathername": "R",
"mark1": 38.0,
"mark2": 40.0,
"mark3": 48.0,
"mark4": 28.0,
"mark5": 50.0
},
{
"_id": ObjectId("5923edf6610be38692e88e4f"),
"stdname": "kumba",
"fathername": "K",
"mark1": 68.0,
"mark2": 60.0,
"mark3": 68.0,
"mark4": 88.0,
"mark5": 35.0
},
{
"_id": ObjectId("5923ee1a610be38692e88e50"),
"stdname": "Jagadesh",
"fathername": "P",
"mark1": 100.0,
"mark2": 90.0,
"mark3": 89.0,
"mark4": 100.0,
"mark5": 95.0
}, {
"_id": ObjectId("5923ee6b610be38692e88e51"),
"stdname": "Dayakar",
"fathername": "R",
"mark1": 70.0,
"mark2": 80.0,
"mark3": 69.0,
"mark4": 60.0,
"mark5": 55.0
}
]
var stdcontinfo = [{
"_id": ObjectId("592437891a87641edc58547e"),
"stdname": "dev1",
"email": "dev1@gmail.com",
"phnum": 6545645645.0,
"__v": 0
},
{
"_id": ObjectId("5924ffe6837e5d2224f42e00"),
"stdname": "Bala",
"email": "Bala@gmail.com",
"phnum": 9965148632.0,
"__v": 0
},
{
"_id": ObjectId("592500371e469423a4f65306"),
"stdname": "kumba",
"email": "kumba@gmail.com",
"phnum": 9962651490.0,
"__v": 0
},
{
"_id": ObjectId("59250085f02d4c0face17820"),
"stdname": "vetri",
"email": "vetri@gmail.com",
"phnum": 9962651490.0,
"__v": 0
},
{
"_id": ObjectId("592500c6c6a6af1d6c7c3fa5"),
"stdname": "Jagadesh",
"email": "Jagadesh@gmail.com",
"phnum": 1234567890,
"__v": 0
}
]
答案 0 :(得分:0)
我认为你不需要Lodash,只需使用简单的JS:
stdJson.forEach(itemA=>{
let resItem = stdcontinfo.find(itemB=>itemA.stdname === itemB.stdname);
if(resItem){
itemA.email = resItem.email;
itemA.phnum = resItem.phnum;
}
});
答案 1 :(得分:0)
使用lodash,您可以使用其中一个交叉函数。没有经过测试,但会是这样的:
var arrayOfSameObjects = _.intersectionBy(stdcontinfo, stdJson, 'stdname');
找到相同的对象后,您可以将其添加到stdJson
:
var stdJson = stdJson.concat(arrayOfSameObjects);
参考文献: