最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript array of arrays -> array[index] is undefined - Stack Overflow

programmeradmin3浏览0评论

I generate some javascript from a php file. In this generated code, I create a multi-dimensional array and then populate it. The first nested array populates without problems but the second ones throws a TypeError: myArray[idx] is undefined.

Here's a code snippet:

function initialize() {
                var arrayLabels = [];
                var arrayMarkers = [];
                var idx = 0;
                arrayMarkers[idx] = [];
                var mapLatlng = new google.maps.LatLng(40.6029248937, 7.7861327300);
                var mapOptions = { center: mapLatlng,
                    zoom: 13, 
                    mapTypeId: google.maps.MapTypeId.SATELLITE };
                var map = new google.maps.Map(document.getElementById("karte"), mapOptions);
                var bounds = new google.maps.LatLngBounds();
                    arrayMarkers[idx]['breite'] = 44.4114053473682;
                    arrayMarkers[idx]['laenge'] = 8.91858100891113;
                    arrayMarkers[idx]['farbe'] = ":8888/img/ico/button2_gruen.png";
                    arrayMarkers[idx]['hafen'] = "Ab/bis Genua";
                    arrayMarkers[idx]['link'] = "Karte&#44; Wetter und<br>Landausfl&uuml;ge f&uuml;r<br><a href='hafen.php?hafen=172'>Genua</a><br>Sa, 16.03.13";
                    idx++;
                    arrayMarkers[idx]['breite'] = 43.3449053146323;

The error is thrown at the last line, right after the index has been incremented. Any ideas what the problem is?

Thanks MK

I generate some javascript from a php file. In this generated code, I create a multi-dimensional array and then populate it. The first nested array populates without problems but the second ones throws a TypeError: myArray[idx] is undefined.

Here's a code snippet:

function initialize() {
                var arrayLabels = [];
                var arrayMarkers = [];
                var idx = 0;
                arrayMarkers[idx] = [];
                var mapLatlng = new google.maps.LatLng(40.6029248937, 7.7861327300);
                var mapOptions = { center: mapLatlng,
                    zoom: 13, 
                    mapTypeId: google.maps.MapTypeId.SATELLITE };
                var map = new google.maps.Map(document.getElementById("karte"), mapOptions);
                var bounds = new google.maps.LatLngBounds();
                    arrayMarkers[idx]['breite'] = 44.4114053473682;
                    arrayMarkers[idx]['laenge'] = 8.91858100891113;
                    arrayMarkers[idx]['farbe'] = "http://new.kfb.localhost:8888/img/ico/button2_gruen.png";
                    arrayMarkers[idx]['hafen'] = "Ab/bis Genua";
                    arrayMarkers[idx]['link'] = "Karte&#44; Wetter und<br>Landausfl&uuml;ge f&uuml;r<br><a href='hafen.php?hafen=172'>Genua</a><br>Sa, 16.03.13";
                    idx++;
                    arrayMarkers[idx]['breite'] = 43.3449053146323;

The error is thrown at the last line, right after the index has been incremented. Any ideas what the problem is?

Thanks MK

Share Improve this question asked Feb 20, 2013 at 9:03 midnigmidnig 1835 silver badges16 bronze badges 5
  • You need to do another arrayMarkers[idx] = []; – RichardTowers Commented Feb 20, 2013 at 9:06
  • @mplungjan That's just an example. The array is called arrayMarkers. – midnig Commented Feb 20, 2013 at 9:10
  • 1 I know, but use the name of the array in the example to not confuse – mplungjan Commented Feb 20, 2013 at 9:10
  • @RichardTowers You mean I should do it every time that the counter increments? – midnig Commented Feb 20, 2013 at 9:11
  • 1 Yes. See @T.J Crowder's answer. – RichardTowers Commented Feb 20, 2013 at 9:17
Add a ment  | 

2 Answers 2

Reset to default 6

You're incrementing idx, and then doing this:

arrayMarkers[idx]['breite'] = 43.3449053146323;

You've never put any object at arrayMarkers[idx], and so you end up trying to add a property to undefined, which causes an error.

If you want to create an array at the new index, add the second line shown here:

idx++;
arrayMarkers[idx] = []; // <=== Add this
arrayMarkers[idx]['breite'] = 43.3449053146323;

Side note: The things you're putting in arrayMarkers[idx] are arrays ([]), but you're not using them as arrays, you're using them as objects. You can do that in JavaScript (because those arrays aren't really arrays at all), but unless you're going to make use of the fact they're arrays, I'd just use objects:

arrayMarkers[idx] = {}; // Instead of []
idx++;
                    arrayMarkers[idx]['breite'] = 43.3449053146323;

should be

                    arrayMarkers[idx]['breite'] = 43.3449053146323;
idx++;

So that you move to next index after all operations are plete. Using them otherwise increments the value to next index which does not exist

Or if it has to stay the same way then you can initialize that value like

idx++;
arrayMarkers[idx] = [];
arrayMarkers[idx]['breite'] = 43.3449053146323;
发布评论

评论列表(0)

  1. 暂无评论