我有这个场景我试图将一个JSON定义映射到另一个,我有一个名为 find 的递归函数,当我进行简单的操作但是当我发送复杂的条件时,它非常有效总是在分配上获得未定义值。
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
答案 0 :(得分:0)
我并不完全符合您的要求,但undefined
与null
不同。在您的代码中,只有在f(elem)
真实时才会返回,因此所有其他情况下结果将为undefined
(因为您没有返回任何其他内容)。如果你的代码失败了一个更复杂的对象,那么它一定不能找到任何东西。
编辑:
在查看您的示例数据后,我看不到id
字段与bpmnElement
匹配的任何位置,因此您的查找功能永远不会找到任何内容。
答案 1 :(得分:0)
尝试:
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;
}
}