0

Doing a project currently using Knockout.JS as my MVVM framework. I'm trying some basic operations such as accessing the values of my particular markersArray object using something simple such as markersArray['Tiong Bahru Station (EWL)']. I understand that I have to use square bracket notation due to the presence of spaces. However, when I console.log this, and further in my code, it has no value.

Code is below:

JS

In scripts.js file:

var stationList = [
        {name: "Tiong Bahru Station (EWL)", marker: markersArray['Tiong Bahru 
Station (EWL)']}
console.log(markersArray['Tiong Bahru Station (EWL)']); // Returns undefined


In maps.js file:

var markersArray = {};
for (var i = 0; i < markers.length; i ++) {
    marker = new google.maps.Marker({
        position: markers[i].position,
        map: map,
        animation: google.maps.Animation.DROP
    });
    var name = markers[i].name;
    markersArray[name] = marker;
}
var markers = [
{name:"Tiong Bahru Station (EWL)", position: {lat:1.28498, lng:103.82283}}
];

I went to check the console.log for my markersArray to ensure that it is built correctly and it is populated correctly. Would appreciate some help in figuring out why I'm not getting any value from just accessing the value using the key.

4
  • This is not enough code to give you any answer. What is the markers array that you iterate over to fill the markersArraty object, which btw is a terrible name for an object. You must also make sure maps.js runs that code before script.js does Commented Jun 15, 2017 at 10:45
  • You seem to have your objects in a bit of a mess. stationList is an Array you are defining, but there is no markersArray defined, so defining stationList fails. In you updated second snippet there is no Array markers so that fails too. I suggest you make one snippet you hope will work and then the community can advise. Commented Jun 15, 2017 at 10:49
  • I've added in new code and yes, maps.js runs before scripts.js. I can console.log(markerArray) in scripts.js and i can see the entire object. Yep, i will rename the object, was playing around so i lost track of my naming conventions. lol Commented Jun 15, 2017 at 10:49
  • @user3094755 i,ve added in new code. Commented Jun 15, 2017 at 10:50

2 Answers 2

1

This snippet sort of works, but I'm not sure what the problem you have is.

The simplest solution would be to put your marker into the makers Array, and then point to the markers Array, with

markers[i].marker = 'The Marker' markers[markers[i].name] = markers[i]

var markers = [
{name:"Tiong Bahru Station (EWL)", position: {lat:1.28498, lng:103.82283}}
];

var markersArray = {};
for (var i = 0; i < markers.length; i ++) {
    var name = markers[i].name;
    markersArray[name] = 'The marker';
}

console.log(JSON.stringify(markers,null,2))

console.log(JSON.stringify(markersArray,null,2))

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, it works if they all run on the same file, but if i try it on scripts.js, it doesn't give a value anymore.
Thanks mate, I figured out the problem.
0

I added an "async" keyword to the script I was loading and that solved the problem. It's because I was waiting for the entire document to load before running the script below it, which caused some issues.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.