0

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
3
  • 2
    What complex things do you send? Example input, please! Also, where are you getting null assignments? Commented May 12, 2014 at 15:28
  • 1
    small note: use 'found' instead of 'finded' Commented May 12, 2014 at 15:29
  • Your condition should better be function(x){ return x.bpmnElement===bpmnId; } - returning the boolean result instead of the value that fulfills the condition. Commented May 12, 2014 at 15:30

2 Answers 2

0

Try with:

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
          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.$.id;});
          var bpmnId = bpmnItem.$.id;//value is ok

            if(bpmnId!=undefined){
              var returnVal;
              find(diagrams,function(x){

                if( x.$ != undefined && x.$.bpmnElement != undefined ){

                  if(x.$.bpmnElement===bpmnId){
                    {returnVal =  x;}
                  }
                }
              });

              console.log("return:"+ returnVal);
          }
          documents.push(mapping);
        }
      }
    }
     return documents;
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, I'm just wondering why the simple assigment didn't work?
0

I don't exactly what you are going for, but undefined is not the same as null. In your code, you only return if f(elem) is truthy, so all other cases the result will be undefined (because you didn't return anything else). If if your code is failing with a more complex object, then it must not be finding anything.

EDIT: after looking at your example data, I don't see anywhere where an id fields matches bpmnElement, so your find function will never find anything.

2 Comments

thanks the condition is x.bpmnElement===bpmnId the bpmnElement property is inside "$" object, the function reach the value and retuns the element in this case "x", when I do the console log in the find function inside the condition I get the object, but when I do another console log outside the function which is suposed that should return the value then I got undefined, do you think that could be an issue related to a scope?
I have updated the console.log with the values I get

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.