2014-10-17 74 views
0

如何從兩個json文檔中找到匹配的數據。例如:我有兩個json文檔和技能json文檔。如何從couchdb中的兩個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"] 
} 
+0

那麼你試圖匹配的數據是什麼? – RobG 2014-10-17 06:44:32

+0

我需要匹配核心技能數據。 – 2014-10-17 06:54:25

+0

您需要詳細說明您要做什麼。 – 2014-10-17 22:23:09

回答

0

我不知道你想做的事,但下面可能會有所幫助。假設第一個數據集的能力及其描述的列表,第二個是僱員記錄,然後分配給有適當的名稱變量可能是這樣的:

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 

以上可能變得更加OO給出構造函數skillCodes員工情況下,我會留給你。

相關問題