0

I have two arrays of objects that I'm trying to feed into a function. One array is a test array that contains 4 objects, the other array contains 1600 objects. The test array is functioning as expected, while the large array is not.

The only difference I can see is here in the console of the browser when logged: enter image description here

Can someone tell me what the difference in these two arrays is?

Here's an expanded pic: enter image description here

Here's the code I'm using to populate the arrays:

let featureGeometriesArray = [];

        function geoJsonPointsToArray(data) {
            // Select just the features of the input GeoJSON data   
            let features = data.features;
            // console.log(features);

            // Create an empty array to hold a lat-lng coordinate array of each feature
            // let featureGeometriesArray = [];

            // Iterate through each feature (f) in features
            features.forEach(function (f) {

                // Create an array containing the latitude and longitude values for the feature and push that array into featureGeometriesArray
                // console.log(f.geometry.coordinates[1])
                // featureGeometriesArray.push([f.geometry.coordinates[1], f.geometry.coordinates[0]])
                featureGeometriesArray.push({"lat":f.geometry.coordinates[1], "lng":f.geometry.coordinates[0]})
            });

            // Return featureGeometriesArray now populated with lat-lng coordinate array for each of the points in the input GeoJSON file
            return featureGeometriesArray
        }

        var testData = {
            max: 8,
            data: [{
                lat: 33.0634804,
                lng: -80.0935456
            }, {
                lat: 33.15065,
                lng: -80.145799
            }, 
            {
                lat: 33.6523106,
                lng: -78.9350132
            },
            {
                lat: 34.645637,
                lng: -82.600961
            }]
        };
        console.log(testData.data)
        var testData = {
            // max: 8,
            data: featureGeometriesArray
        };

        // console.log(featureGeometriesArray)
        console.log(testData.data.length)
Matt
  • 967
  • 2
  • 9
  • 23
  • The console is trying to be helpful. If you click on the little "arrow" things the arrays will have pretty much the same appearance. – Pointy Oct 25 '19 at 16:41
  • Are you having some actual problem? The console does whatever it wants; it's not necessarily consistent in the way it shows you things. – Pointy Oct 25 '19 at 16:45
  • 2
    Don't get too hung up on what it looks like in the console. Worry more about what data are showing up when you attempt to access it from code. If you want help with the latter, show how you're filling and accessing the array. – Heretic Monkey Oct 25 '19 at 16:46
  • posting code now – Matt Oct 25 '19 at 16:47
  • how did you fill the 1600 items array ? does `bigArray.length` gives the expected number of items ? – Patrick Ferreira Oct 25 '19 at 16:47
  • @PatrickFerreira that returns 0! What does that mean? – Matt Oct 25 '19 at 16:48
  • About what the console is showing, you may want to read [this answer to "weird array behaviour in javascript"](https://stackoverflow.com/a/49838747/215552). – Heretic Monkey Oct 25 '19 at 16:50
  • 4
    I'm guessing that you're filling your array with the result of an ajax call. That would cause the behavior you're seeing, since the array would be empty at the time you `console.log`ged it, but by the time you clicked the arrow, it was populated. – Heretic Monkey Oct 25 '19 at 16:54
  • yes, it is an ajax call. That may be the source of my problem as well. – Matt Oct 25 '19 at 16:56
  • @Matt it happens when you add items in an array with a string instead of a number : `bigArray[''] = 'my data'` it is better to push items `bigArray.push('my data')` or you should be sure you are using number `bigArray[0] = 'my data'` – Patrick Ferreira Oct 25 '19 at 17:02
  • @Matt did you forget to call the method `geoJsonPointsToArray()` – Patrick Ferreira Oct 25 '19 at 17:08
  • @PatrickFerreira no that's what's populating the large array – Matt Oct 25 '19 at 17:09
  • As @HereticMonkey suggests, it could be a _delay_ issue, try to console log in the sucess callback of you ajax call – Patrick Ferreira Oct 25 '19 at 17:18
  • Just a note: you can shorten your `geoJsonPointsToArray` to just `return data.features.map(f => ({"lat":f.geometry.coordinates[1], "lng":f.geometry.coordinates[0]}));` If you don't need to support older browsers that is :). – Heretic Monkey Oct 25 '19 at 17:30

0 Answers0