在node.js中调用递归函数时为空值 [英] Null value when calling a recursive function in node.js
本文介绍了在node.js中调用递归函数时为空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在这种情况下,我试图将一个JSON定义映射到另一个,我有一个名为 find 的递归函数,当我执行简单操作时,该函数很好用,但是当我发送复杂条件时,我总是得到分配中的 undefined 值.
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
}
}
}
控制台输出如下:
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
等,等等...
输入数据的范例
"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
推荐答案
尝试使用:
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;
}
}
这篇关于在node.js中调用递归函数时为空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文