• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

176
Views
Estoy tratando de evitar que un diccionario constante se modifique en una función

Estoy escribiendo un programa de mapa interior de edificio de JavaScript University, y el código de ruta de visualización funciona, sin embargo, mi diccionario que almacena el gráfico se está cambiando dentro de la función y no tengo mucha experiencia en JavaScript y no puedo detectar el problema.

este es el Diccionario;

 const TheGraph = { "A" : { "Coordinates" : [3776, 625], "Links" : {"C": 1} }, "B" : { "Coordinates" : [970, 910], "Links" : {"C" : 5, "G" : 5} }, .... }

Y aquí es donde creo que está el problema.

 button.addEventListener('click', () =>{ var possRoutes = []; // stores the points const lineString = new H.geo.LineString(); var heading = (document.getElementById('room-header')).innerHTML var heading = heading.substring(5); // gets the name of the room for (const x of (Rooms[heading][1])){ possRoutes.push(findShortestPath(TheGraph, "A", x)) } // finds the shorted path possRoutes.reduce(function(prev, curr) { return prev.distance < curr.distance ? prev : curr; }); for (var cor in possRoutes[0].path){ var point = TheGraph[point = possRoutes[0].path[cor]].Coordinates // gets the coordinate from the dictionary var distanceToOrigin = pythagorean(point[1], point[0])//maths var ang = 1.5708-(angle + Math.atan(point[0]/point[1])) point[0] = originX-(Math.cos(ang)*distanceToOrigin)*scaleX point[1] = originY-(Math.sin(ang)*distanceToOrigin)*scaleY lineString.pushPoint({lat: point[0], lng: point[1]}) // adds the point to the map } point = Rooms[heading][0] var distanceToOrigin = pythagorean(point[1], point[0]) var ang = 1.5708-(angle + Math.atan(point[0]/point[1])) point[0] = originX-(Math.cos(ang)*distanceToOrigin)*scaleX point[1] = originY-(Math.sin(ang)*distanceToOrigin)*scaleY lineString.pushPoint({lat: point[0], lng: point[1]}) console.log(TheGraph) path = new H.map.Polyline( lineString, { style: { lineWidth: 4, strokeColor: '#2600ff' }} ) F0.addObject(path);//displays the route on the map })
about 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

const significa que el nombre theGraph no se puede reasignar para referirse a otra cosa, no que la cosa a la que se refiere sea inmutable. Está cambiando los elementos en la matriz de coordenadas en las líneas que comienzan con point[0] etc.

about 3 years ago · Juan Pablo Isaza Report

0

Esto solucionó mi problema.

var point = Object.assign({}, point);

https://stackoverflow.com/a/29050089/18367450

about 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error