Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
¿Cómo hago que esta función esté orientada a objetos para poder agregar una nueva ubicación a mi objeto y que funcione en mi función?

Este es mi js:

 var animalAgeValue; var animalHabitatValue; var latLong = { Berlin: {lat:52.5159, lng:13.3777}, SanFrancisco: {lat:37.773972, lng:-122.431297}, Beijing: {lat:39.916668, lng:116.383331} } function goToPage(page) { if(page == "p1") { document.getElementById("p1").hidden = false document.getElementById("map").hidden = true document.getElementById("p2").hidden = true document.getElementById("newAnimal").hidden = true } else if(page == "p2") { document.getElementById("p1").hidden = true document.getElementById("map").hidden = false document.getElementById("p2").hidden = false document.getElementById("newAnimal").hidden = false document.getElementById("animalNameD").innerHTML = animalNameValue; document.getElementById("animalAgeD").innerHTML = animalAgeValue; document.getElementById("animalHabitatD").innerHTML = animalHabitatValue } } function submitted() { animalNameValue = document.getElementById("animalName").value; console.log(animalNameValue); animalAgeValue = document.getElementById("animalAge").value; console.log(animalAgeValue); const habitats = document.querySelectorAll('input[name="habitat"]'); for (const habitat of habitats) { if (habitat.checked) { animalHabitatValue = habitat.value; console.log(animalHabitatValue); if(habitat.value == "Berlin") { map.setCenter(latLong.Berlin); map.setZoom(9) } else if(habitat.value == "San-Francisco") { map.setCenter(latLong.SanFrancisco); map.setZoom(9) } else if(habitat.value == "Beijing") { map.setCenter(latLong.Beijing); map.setZoom(9) } break; } } goToPage("p2") } var submit = document.querySelector('#submit') submit.onclick = function() {submitted()};

Quiero reformatear mi función submitted para que funcione si agrego una nueva ubicación en mi objeto latLong y agrego el elemento en mi HTML y lo selecciono en mi página web, centraría mi mapa alrededor de las ubicaciones de latitud y longitud. En otras palabras, quiero pasar nuevos valores de latitud y longitud a map.setCenter desde el objeto latLong sin tener que agregar una función if adicional cada vez.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

puede acceder a las propiedades del objeto a través de la sintaxis de corchetes utilizando una variable como nombre de propiedad.

map.setCenter(latLong[habitat.value]);

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!