I have this scenario I'm trying to map one JSON definition to another, I have a recursive function named find which works perfectly when I do simple operations but when I send complex conditions I always get a undefined value over the assigment.
var mapToModel = function(res){
var processes = res.definitions.process;
var diagrams = res.definitions.BPMNDiagram;
var documents = [];
for(var prop in processes) {
if(processes.hasOwnProperty(prop)){
var propertyNames = Object.getOwnPropertyNames(processes[prop]);
for(var property in processes[prop]){
var mapping ={};
if(property==="$"){
//do something with the process
}else{
//shapes
console.log("\n");
mapping.id = new mongo.ObjectID();
mapping.hash = hash.hashCode(new Date().toString());
mapping.type = property;
mapping.value = processes[prop][property];
var bpmnItem = find(processes[prop][property], function(x) {return x;});
var bpmnId = bpmnItem.$.id;
console.log("bpmnId: "+bpmnId);
if(bpmnId!=undefined){
var returnVal = find(diagrams,function(x){
if(x.bpmnElement===bpmnId){
console.log("element found: " + JSON.stringify(x,null,4));
return x;
}
});
console.log("returned value: "+returnVal);
}
documents.push(mapping);
}
}
}
return documents;
}
}
function find(items,f) {
for(var key in items) {
var elem = items[key];
if (f(elem)) { console.log(JSON.stringify(elem,null,4)); return elem;}
if(typeof elem === "object") {
find(elem,f); // call recursively
}
}
}
Edit:
the console output is the following:
bpmnId: StartEvent_1
element found: {
"id": "BPMNShape_1",
"bpmnElement": "StartEvent_1"
}
returned value: undefined
bpmnId: SequenceFlow_9
element found: {
"id": "BPMNEdge_SequenceFlow_10",
"bpmnElement": "SequenceFlow_9",
"sourceElement": "BPMNShape_1",
"targetElement": "BPMNShape_Task_3"
}
returned value: undefined
etc, etc...
an exmaple of the input data
"BPMNDiagram": [
{
"$": {
"id": "BPMNDiagram_1",
"name": "procurement subprocess"
},
"BPMNPlane": [
{
"$": {
"id": "BPMNPlane_1",
"bpmnElement": "process"
},
"BPMNShape": [
{
"$": {
"id": "BPMNShape_1",
"bpmnElement": "StartEvent_1"
},
"Bounds": [
{
"$": {
"height": "36.0",
"width": "36.0",
"x": "20.0",
"y": "182.0"
}
}
]
},
{
"$": {
"id": "BPMNShape_Task_3",
"bpmnElement": "Task_2"
},
"Bounds": [
{
"$": {
"height": "50.0",
"width": "110.0",
"x": "100.0",
"y": "175.0"
}
}
]
},
{
"$": {
"id": "BPMNShape_ExclusiveGateway_2",
"bpmnElement": "ExclusiveGateway_2",
"isMarkerVisible": "true"
},
"Bounds": [
{
"$": {
"height": "50.0",
"width": "50.0",
"x": "250.0",
"y": "175.0"
}
}
]
}// it continues