2017-03-02 35 views
0

我有2個物體加入2個陣列由鍵和值(AngularJS)

{ 
    "_id": "58b7f36b3354c24630f6f3b0", 
    "name": "refcode", 
    "caption": "Reference", 
    "type": "string", 
    "search": false, 
    "required": false, 
    "table": true, 
    "expansion": true 
}, 

{ 
    "_id": "58b7f36b3354c24630f6f3c8", 
    "vacancyid": "0", 
    "refcode": "THIS IS MY REF", 
    "position": "Test", 
    "jobtype": "Temp", 
    "department": "Industrial", 
    "branch": "Office", 
    "startdate": "02/12/2013", 
    "contactname": "Person Name", 
    "contactemail": "[email protected]", 
    "Q_V_TYP": "Daily", 
    "score": 0 
}, 

對象一個限定字段應該是什麼以及它被稱爲

的第二個對象是工作描述。

我需要的是一個字段匹配的每個鍵(這甚至聽起來我我的頭混亂,所以這裏有一個例子)

{ 
    "_id": "58b7f36b3354c24630f6f3c8", 
    "vacancyid": "0", 
    "refcode": { 
    "_id": "58b7f36b3354c24630f6f3b0", 
    "name": "refcode", 
    "caption": "Reference", 
    "type": "string", 
    "search": false, 
    "required": false, 
    "table": true, 
    "expansion": true, 
    "value": "THIS IS MY REF" 
    } 
    },  
    "position": "Test", 
    "jobtype": "Temp", 
    "department": "Industrial", 
    "branch": "Office", 
    "startdate": "02/12/2013", 
    "contactname": "Person Name", 
    "contactemail": "[email protected]", 
    "Q_V_TYP": "Daily", 
    "score": 0 
}, 
+0

綁定一個對象與另一個對象的關鍵是什麼?我的意思是,什麼是外鍵 – yBrodsky

+0

與第二個對象鍵相匹配的「名稱」的第一個對象值 - 因此在該示例中,refcode與匹配2 – Gaza

+0

然後有什麼問題,如果您有辦法匹配它們什麼阻止你這樣做? – yBrodsky

回答

1

在這裏你去:

var def = { 
 
    "_id": "58b7f36b3354c24630f6f3b0", 
 
    "name": "refcode", 
 
    "caption": "Reference", 
 
    "type": "string", 
 
    "search": false, 
 
    "required": false, 
 
    "table": true, 
 
    "expansion": true 
 
}; 
 

 
var jobDesc = { 
 
    "_id": "58b7f36b3354c24630f6f3c8", 
 
    "vacancyid": "0", 
 
    "refcode": "THIS IS MY REF", 
 
    "position": "Test", 
 
    "jobtype": "Temp", 
 
    "department": "Industrial", 
 
    "branch": "Office", 
 
    "startdate": "02/12/2013", 
 
    "contactname": "Person Name", 
 
    "contactemail": "[email protected]", 
 
    "Q_V_TYP": "Daily", 
 
    "score": 0 
 
}; 
 

 
var jobDescKeysArr = Object.keys(jobDesc); 
 

 
if (jobDescKeysArr.indexOf(def.name) !== -1) { 
 
    // A match. 
 
    def.value = jobDesc[def.name]; 
 

 
    jobDesc[def.name] = Object.assign({}, def); 
 

 
    console.log(jobDesc) 
 
}