は、私は、次のコードを持っている:クローンされた配列をJavaScriptでスプライスすると、元の配列がスプライスされるのはなぜですか?
var coords = [
{lat: 39.57904, lng: -8.98094, type: "a"}, // A
{lat: 39.55436, lng: -8.95493, type: "b"}, // B
{lat: 39.56634, lng: -8.95836, type: "c"} // C
];
var travelingOptions = [];
getAllTravelingOptions();
function getAllTravelingOptions(){
coords.forEach((point, pos) => {
let c = coords;
delete c[pos];
console.log(c);
console.log(coords);
});
}
なぜそれが変数c
とcoords
は常に同じであるということですか? c
を削除すると、そのアクションはcoords
に反映されます。これは正常な動作ですか?
'C'と 'coords'両方とも同じオブジェクトへの参照です。 – Amy