2016-10-10 60 views
1
{ 
    "childA": 156, 
    "childB": 2422, 
    "color": "df7f00", 
    "id": 124, 
    "posA": { 
     "x": 5, 
     "y": -46, 
     "z": 11 
    }, 
    "posB": { 
     "x": 5, 
     "y": -46, 
     "z": 11 
    }, 
    "shapeId": "4a1b886b-913e-4aad-b5b6-6e41b0db23a6", 
    "xaxisA": 1, 
    "xaxisB": 1, 
    "zaxisA": 2, 
    "zaxisB": 2 
} 

首先是一些重要的上下文。在JavaScript中玩数组。所以我有一个数组“物体”的对象,其中有一个“孩子”的数组。上面张贴的代码是其中的一个孩子。当每个元素可以引用数组中的索引时移除数组元素

现在我已经写了一些JavaScript来删除基于shapeId与.filter()方法的某些孩子,但这会产生一个问题与“childA”和“childB”值指向某个特定的索引孩子们阵列。值得注意的是,并非childs数组中的所有元素都具有child *属性。我很难考虑如何完成删除一个元素,然后修复childs数组中其他元素的“childA”,“childB”等值作为结果抛出。

我想我需要检查子元素之前删除的元素引用子元素后删除的元素和调整,以及子元素之后引用一个孩子的删除元素。我还需要删除对从数组中移除的元素的引用。我不知道如何完成这个。

请原谅我的无知,因为我正在学习,并感谢您的任何帮助。

下面是与前几个孩子的对象:

{ 
"bodies": [{ 
    "childs": [{ 
     "color": "560202", 
     "controller": { 
      "audioIndex": 1, 
      "controllers": null, 
      "id": 28253, 
      "joints": null, 
      "pitch": 1, 
      "volume": 100 
     }, 
     "pos": { 
      "x": -13, 
      "y": -18, 
      "z": 3 
     }, 
     "shapeId": "4c6e27a2-4c35-4df3-9794-5e206fef9012", 
     "xaxis": 2, 
     "zaxis": 1 
    }, { 
     "color": "0a1d5a", 
     "controller": { 
      "audioIndex": 1, 
      "controllers": null, 
      "id": 28248, 
      "joints": null, 
      "pitch": 1, 
      "volume": 100 
     }, 
     "pos": { 
      "x": 16, 
      "y": -13, 
      "z": 3 
     }, 
     "shapeId": "4c6e27a2-4c35-4df3-9794-5e206fef9012", 
     "xaxis": 1, 
     "zaxis": -2 
    }, 
+0

它不是一个对象吗? –

+1

请加上数组,你正在谈论。 –

+0

如果这些是索引值而不是id值,那么您可能必须更新删除元素后面的所有元素才能使用索引-1。 – Malk

回答

0

就个人而言,我会避免直接使用索引,因为它可能会非常棘手,让他们跟上时代的一个可靠的途径;一种可能的选择是将一些'id'字段添加到对象中,以便可以独立于阵列上的位置访问它;您也可以拥有一个包含ID作为键的辅助对象,并且该对象将自己引用为值,以加快访问速度。

数据结构化的方式dos看起来不太适合编辑,所以如果意图使得某些东西变得非常简单,直接对原始结构直接进行激活才会是可行的。对于更复杂的编辑,将其转换为内部更方便的表示形式,在其上进行更改,然后将其转换回原始表示形式,实际上可以节省调试头痛的时间。

+0

该对象实际上是由视频游戏创建的,作为创作的蓝图,所以我无法改变一个孩子如何引用另一个。 –

+0

我明白了......那么,你打算如何使用这个对象呢?由于其结构化的方式,它不适合编辑,所以如果你正在做一些非常简单的事情,直接对原始结构进行激励只会是可行的;如果您打算进行复杂的编辑,然后将编辑后的结果保存回来,将其转换为内部更方便的表示形式,然后再进行修改,然后将其转换回原始表示形式,这实际上可以节省您的时间。 –

+1

总的来说,我的意图是从childs数组中删除所有使用childs数组上的.filter方法的项目,但它一直让我在游戏中出现无效的蓝图。将问题追查到那些childA (childB等..)属性,没有指向任何Id –

0

只是想发布我目前使用的代码,现在它实际上提供了一个有效的蓝图。我希望能够对你自己的问题发表一个答案并不是坏习惯,但它现在正在工作,我渴望分享它。有处理这种可能更加有效的方式,但这里是我得到了什么:

function deleteObjects() { 
var IdCleanup = new Array(); 
objectCount = 0; 
var cleanupCount = 0; 
var jointCleanupCount = 0; 
var oChildIndex = 0; 

var childACleanup = 0; 
var childBCleanup = 0; 
var childCCleanup = 0; 

// Loop through the array manually and remove the id of elements being removed from thejson.bodies[i].childs[j].controller.controllers[*].id 
// fruits.splice(index, howmany); 

/* for(var i = 0; i < thejson.bodies.length; i++) { thejson.bodies[i].childs = thejson.bodies[i].childs.filter(checkObject); } */ 


/*************************************************************************************************************** 
To do list: 
1. Add thejson.bodies[*].childs[*].controller.id to IdCleanup list. 
2. Remove thejson.bodies[*].childs[*] 
3. Adjust thejson.joints[*].childA, thejson.joints[*].childB, .... references. 

4. Remove thejson.bodies[*].childs[*].controller.controllers[*].id values in the IdCleanup list. 
***************************************************************************************************************/ 



// One pass to delete the child objects, and add the deleted child's IDs to list. 
for(var i = 0; i < thejson.bodies.length; i++) { 
    for(var j = 0; j < thejson.bodies[i].childs.length; j++) { 

     // Shape matches dropdown menu value. 
     if(thejson.bodies[i].childs[j].shapeId == document.getElementById("mySelect").value) { 

      if(IdCleanup.indexOf(thejson.bodies[i].childs[j].controller.id) == -1) { 
       // Add this ID to the list of ones to remove. 
       IdCleanup[IdCleanup.length] = thejson.bodies[i].childs[j].controller.id; 
      } 

      // Remove this child. 
      thejson.bodies[i].childs.splice(j, 1); 
      j--; 
      oChildIndex--;// Keeping the overall index accurate. 

      // Adjust joints array. 
      if(thejson.joints != undefined) { 
       for(var k = 0; k < thejson.joints.length; k++) { 
        if(thejson.joints[k].childA) { 
         var idx = thejson.joints[k].childA; 

         if(idx < oChildIndex) { 

         } else if(idx > oChildIndex) { 
          thejson.joints[k].childA--; 
          jointCleanupCount++; 
          childACleanup++; 
         } else if(idx == oChildIndex) { 

         } 
        } 

        if(thejson.joints[k].childB) { 
         var idx = thejson.joints[k].childB; 

         if(idx < oChildIndex) { 

         } else if(idx > oChildIndex) { 
          thejson.joints[k].childB--; 
          jointCleanupCount++; 
          childBCleanup++; 
         } else if(idx == oChildIndex) { 

         } 
        } 

        if(thejson.joints[k].childC) { 
         var idx = thejson.joints[k].childC; 

         if(idx < oChildIndex) { 

         } else if(idx > oChildIndex) { 
          thejson.joints[k].childC--; 
          jointCleanupCount++; 
          childCCleanup++; 
         } else if(idx == oChildIndex) { 

         } 
        } 
       } 
      } 
      objectCount++; 
     } 

     oChildIndex++; 
    } 
} 

// Second pass for cleaning up the IDs from thejson.bodies[i].childs[j].controller.controllers that need removed. 
for(var i = 0; i < thejson.bodies.length; i++) { 
    for(var j = 0; j < thejson.bodies[i].childs.length; j++) { 
     var object = thejson.bodies[i].childs[j]; 

     // Check for controllers 
     if(thejson.bodies[i].childs[j].controller && thejson.bodies[i].childs[j].controller.controllers != null) { 
      for(var k = 0; k < thejson.bodies[i].childs[j].controller.controllers.length; k++) { 

       if(IdCleanup.indexOf(thejson.bodies[i].childs[j].controller.controllers[k].id) != -1) { 
        // one of the controllers match an ID of one that was deleted. 

        var idx = IdCleanup.indexOf(thejson.bodies[i].childs[j].controller.controllers[k].id); 

        thejson.bodies[i].childs[j].controller.controllers.splice(k,1); 
        k--; 
        cleanupCount++; 
       } 
      } 
     } 
    } 
} 

var processLog = document.getElementById('processLog'); 
processLog.value = processLog.value +"\n"+ objectCount +" objects deleted.\n"; 
processLog.value = processLog.value + cleanupCount +" ID cleanup operations.\n"; 
processLog.value = processLog.value + jointCleanupCount +" joint cleanup operations.\n"; 
processLog.value = processLog.value + childACleanup +" A, "+ childBCleanup +" B, "+ childCCleanup +" C\n"; 

}

本来这只是一个通过体阵列循环,然后使用.filter各的childs数组检查相应的shapeId。这会导致关节数组(与body(& childs)数组分开)指向childs数组中的错误索引。 Haroldo_OK让我走上正确的轨道来解决这个问题。几分钟前,它打我,我没有保持我的整体孩子数(oChildCount:P)准确时,我会删除一个孩子。所以我每次删除一个孩子时都会减少这个数字!有用!

非常感谢,Haroldo_OK