2016-07-13 1 views
0

Ich habe ein Array wie folgt aus:Wie ändert man die Werte der Kinder eines Arrays von Objekten?

const mockObjPanoramas = [{ 
    'id': '7oADHnGZcr', 
    'appMarkers': [{ 
     'panoId': '7oADHnGZcr', 
     'color': 'red', 
     'x': 0, 
     'y': 0, 
     'z': 1000 
    }, { 
     'panoId': '8szmQ8ELKs', 
     'color': 'red', 
     'x': 100, 
     'y': 0, 
     'z': 1000 
    }] 
    }, { 
    'id': '8szmQ8ELKs', 
    'appMarkers': [{ 
     'panoId': '7oADHnGZcr', 
     'color': 'green', 
     'x': 0.1352234, 
     'y': -0.2600403, 
     'z': 0.9960099 
    }, { 
     'panoId': '8szmQ8ELKs', 
     'color': 'green', 
     'x': 0.03900146, 
     'y': 0.295959, 
     'z': 0.9169907 
    }] 
    }] 

Ich möchte jedem appMarker die folgende Formel anzuwenden und ein neues Array zurück:

(x, y, z) = (-z, y, -x) * 1000 

Also tat ich dies:

function parseUnityMarkers (mockObjPanoramas) { 
    return mockObjPanoramas.map(mockObjPanorama => { 
    return mockObjPanorama.appMarkers.map(appMarker => { 
     const depth = 1000 
     appMarker.x = -appMarker.z * depth 
     appMarker.y = appMarker.y * depth 
     appMarker.z = -appMarker.x * depth 
    }) 
    }) 
} 

Allerdings bekomme ich folgendes: enter image description here

Was mache ich falsch?

HINWEIS: Ich möchte ein neues Array zurückgeben, das identisch mit dem ursprünglichen ist. Aber mit den Werten von x, y und z der appMarkers geändert.

+0

würden Sie das ursprüngliche Objekt ändern oder ein neues unabhängiges Ergebnis zurückgeben? –

+0

@Nina Scholz Rückgabe eines neuen Ergebnisses. Identisch wie das Original, aber mit dem Wert der "appMarkers" geändert. – alex

+0

Ein zweiter Abschnitt hinzugefügt, der Ihre ursprünglichen Daten beibehalten sollte. – TbWill4321

Antwort

1

Machte es einfach (Ich mag einfache Art und Weise, Dinge zu tun in :)):

function parseUnityMarkers(mockObjPanoramas) { 
     for(var i=0; i < mockObjPanoramas.length; i++){ 
     var temp1 = mockObjPanoramas[i]; 
     for(var j=0; j < temp1.appMarkers.length; j++) { 
      var temp2 = temp1.appMarkers[j]; 
      const depth = 1000; 
      var x = temp2.x; 
      temp2.x = -temp2.z * depth 
      temp2.y = temp2.y * depth 
      temp2.z = -x * depth 
     } 
     } 
     return mockObjPanoramas; 
    } 

Eine wichtige Sache:

wenn Sie tun:

appMarker.x = -appMarker.z * depth 
    appMarker.y = appMarker.y * depth 
    appMarker.z = -appMarker.x * depth 

Beachten Sie, dass Sie machte den z Wert als: -z * depth * depth , weil Sie schon x durch -z * depth ersetzten.

const mockObjPanoramas = [{ 
 
    'id': '7oADHnGZcr', 
 
    'appMarkers': [{ 
 
    'panoId': '7oADHnGZcr', 
 
    'color': 'red', 
 
    'x': 0, 
 
    'y': 0, 
 
    'z': 1000 
 
    }, { 
 
    'panoId': '8szmQ8ELKs', 
 
    'color': 'red', 
 
    'x': 100, 
 
    'y': 0, 
 
    'z': 1000 
 
    }] 
 
}, { 
 
    'id': '8szmQ8ELKs', 
 
    'appMarkers': [{ 
 
    'panoId': '7oADHnGZcr', 
 
    'color': 'green', 
 
    'x': 0.1352234, 
 
    'y': -0.2600403, 
 
    'z': 0.9960099 
 
    }, { 
 
    'panoId': '8szmQ8ELKs', 
 
    'color': 'green', 
 
    'x': 0.03900146, 
 
    'y': 0.295959, 
 
    'z': 0.9169907 
 
    }] 
 
}]; 
 

 
function parseUnityMarkers(mockObjPanoramas) { 
 
    for(var i=0; i < mockObjPanoramas.length; i++){ 
 
    var temp1 = mockObjPanoramas[i]; 
 
    for(var j=0; j < temp1.appMarkers.length; j++) { 
 
     var temp2 = temp1.appMarkers[j]; 
 
     const depth = 1000; 
 
     var x = temp2.x; 
 
     temp2.x = -temp2.z * depth 
 
     temp2.y = temp2.y * depth 
 
     temp2.z = -x * depth 
 
    } 
 
    } 
 
    return mockObjPanoramas; 
 
} 
 

 
console.log(parseUnityMarkers(mockObjPanoramas));

1

Ich glaube, du bist eine return-Anweisung aus Ihrer zweiten Karte Aufruf fehlen:

function parseUnityMarkers (mockObjPanoramas) { 
    return mockObjPanoramas.map(mockObjPanorama => { 
    return mockObjPanorama.appMarkers.map(appMarker => { 
     const depth = 1000 
     appMarker.x = -appMarker.z * depth 
     appMarker.y = appMarker.y * depth 
     appMarker.z = -appMarker.x * depth 
     return appMarker; 
    }) 
    }) 
} 

aber Sie gehen in ein anderes Problem laufen. Sie überschreiben den Wert appMarker.x und versuchen anschließend, auf den ursprünglichen Wert zuzugreifen. Außerdem überschreiben Sie Ihre ursprünglichen Daten. Sie können Ihre Originaldaten aus wechselnden so weiter:

function parseUnityMarkers (mockObjPanoramas) { 
    return mockObjPanoramas.map(mockObjPanorama => { 
    return { 
     id: mockObjPanorama.id, 
     appMarkers: mockObjPanorama.appMarkers.map(appMarker => { 
     const depth = 1000 
     return { 
      panoId: appMarker.panoId, 
      color: appMarker.color, 
      x: -appMarker.z * depth, 
      y: appMarker.y * depth, 
      z: -appMarker.x * depth 
     }; 
     }) 
    } 
    }) 
} 
+0

Danke für die Antwort. Und mir wurde klar, dass die Eltern der Kinder weg sind! – alex

+0

Ich habe eine Änderung vorgenommen, so dass die gesamte Datenstruktur dupliziert werden soll, auch die Eltern. – TbWill4321

Verwandte Themen