1

配列内の en 要素のインデックスを取得しようとしています。

これは完全に機能します

        var onCreate = function (event) {
        console.assert(markers[markerId] === undefined);

        var markerId = event.markerId;
        markers[markerId] = {};

        var marker = markers[markerId];
        // create the container object
        marker.object3d = new THREE.Object3D();
        marker.object3d.matrixAutoUpdate = false;
        scene.add(marker.object3d);

        //UPDATE ARRAY HOLDING CURRENT MARKERS
        console.log("ON CREATE");
        console.log("current detected markers: " + currentMarkers);
        var idx = currentMarkers.indexOf(markerId); // Find the index
        console.log("marker " + event.markerId + " has index: " + idx);
        if(idx==-1) // if does not exist
            currentMarkers.push(markerId);

しかし、これはいけません...

    var onDelete = function (event) {

        console.assert(markers[event.markerId] !== undefined);
        var markerId = event.markerId;
        //UPDATE ARRAY HOLDING CURRENT MARKERS
        console.log("ON DELETE");
        console.log("current detected markers: " + currentMarkers);
        var idxx = currentMarkers.indexOf(markerId); // Find the index
        console.log("marker " + markerId + " has index: " + idxx);
        if(idxx != -1) {// if DOES exist
            currentMarkers.splice(idxx, 1); //Delete
            var idxxx = currentMarkersCoverChecked.indexOf(markerId); // Find the index
            if(idxxx == -1) // if does NOT exist, so if not being checked
                checkMarkerCovered(markerId);
        }
        onDeleteRandom(markerId);
        var marker = markers[markerId];
        scene.remove(marker.object3d);
        delete markers[markerId];

私のコンソール出力を見てください:

ON CREATE 
current detected markers: 2,1,4 
marker 3 has index: -1 
ON CREATE main.js:266
current detected markers: 2,1,4,3 
marker 4 has index: 2 
ON DELETE 
current detected markers: 2,1,4,3
marker 2 has index: -1 
ON CREATE 
current detected markers: 2,1,4,3 
marker 2 has index: 0 
ON DELETE 
current detected markers: 2,1,4,3 
marker 1 has index: -1 
4

2 に答える 2

1

値の型を確認しましたかmarkerId

JavaScript 配列を使用すると、型IntegerだけでなくString型とObject型をに挿入できますArray。特定の値がタイプIntegerの場合、試してみるとindexOf("stringvalue");-1 as return value

これが問題の解決に役立つことを願っています!

于 2012-10-16T13:21:08.973 に答える
0

currentMarkersCoverChecked定義されていないため、2 番目の例は機能しません。

于 2012-10-16T13:09:30.907 に答える