如何从两个json documents.For中找到匹配的数据例如:我有两个json文档和技能json文档。
在技能文档中:
{
"_id": "b013dcf12d1f7d333467b1447a00013a",
"_rev": "3-e54ad6a14046f809e6da872294939f12",
"core_skills": [
{
"core_skill_code": "SA1",
"core_skill_desc": "communicate with others in writing"
},
{
"core_skill_code": "SA2",
"core_skill_desc": "complete accurate well written work with attention to detail"
},
{
"core_skill_code": "SA3",
"core_skill_desc": "follow guidelines/procedures/rules and service level agreements"
},
{
"core_skill_code": "SA4",
"core_skill_desc": "ask for clarification and advice from others"
}
]}在员工文档中:
{
"_id": "b013dcf12d1f7d333467b12350007op",
"_rev": "3-e54ad6a14046f809e6da156794939f12",
"employee_name" :"Ashwin",
"employee_role" : "Software engineer",
"core_skills":["SA1","SA4"]
}发布于 2014-10-18 06:45:34
我不知道你想做什么,但以下可能会有所帮助。假设第一个数据集是技能及其描述的列表,第二个是员工记录,那么为具有合适名称的变量赋值可能如下所示:
var skillCodes = {
"_id": "b013dcf12d1f7d333467b1447a00013a",
"_rev": "3-e54ad6a14046f809e6da872294939f12",
"core_skills": [{
"core_skill_code": "SA1",
"core_skill_desc": "communicate with others in writing"
},{
"core_skill_code": "SA2",
"core_skill_desc": "complete accurate well written work with attention to detail"
},{
"core_skill_code": "SA3",
"core_skill_desc": "follow guidelines/procedures/rules and service level agreements"
},{
"core_skill_code": "SA4",
"core_skill_desc": "ask for clarification and advice from others"
}
]};
var employee0 = {
"_id": "b013dcf12d1f7d333467b12350007op",
"_rev": "3-e54ad6a14046f809e6da156794939f12",
"employee_name" :"Ashwin",
"employee_role" : "Software engineer",
"core_skills":["SA1","SA4"]
};创建技能索引可以使查找特定技能变得更加简单,执行此操作的一些代码如下:
var skillCodeIndex = {};
skillCodes.core_skills.forEach(function(item){
skillCodeIndex[item.core_skill_code] = item.core_skill_desc;
});现在所需要的只是一个函数来获取特定员工的技能,比如:
function getCoreSkills (employee) {
console.log('Employee ' + employee.employee_name + ' has the following core skills:');
employee.core_skills.forEach(function(skill) {
console.log(skill + ': ' + skillCodeIndex[skill]);
});
}举个例子:
getCoreSkills(employee0);
Employee Ashwin has the following core skills:
SA1: communicate with others in writing
SA4: ask for clarification and advice from others如果给出skillCodes和employee实例的构造函数,上面的内容可能会变得更加面向对象,我将把它留给您。
https://stackoverflow.com/questions/26419061
复制相似问题