5

I'm just ripping my hair out over this code! I have to be missing something obvious. I declare an array, and its type returns "object!"

code follows:

var markers = new Array();
console.log(typeof markers);
d3.json(queryUrl, function(data) {
    console.log(data.features)
    data.features.forEach( function(each) {
        let ball = each["geometry"]["coordinates"]
        console.log(typeof markers)
        markers.push(
            L.marker([ball[1], ball[0]])
            .bindPopup(`Magnitude: ${each["properties"]["mag"]}`);
        )
    });
});

markers = L.layerGroup(markers);

in both console.log(typeof)'s, they return object, and the markers.push() line raises an error (which is what prompted checking the type in the first place)

my first, knee-jerk reaction was that last line was the culprit, but I changed the first line to var test = new Array(); and it did the same thing

anothermh
  • 9,815
  • 3
  • 33
  • 52
  • 2
    typeof Array will always be an object ... because all (most) non-primitive's in javascript are objects – Jaromanda X May 15 '18 at 00:19
  • What is the error? – 4castle May 15 '18 at 00:19
  • @AndroidNoobie - not really - because OP claims that `.push` is rasing an error – Jaromanda X May 15 '18 at 00:21
  • @4castle "Uncaught TypeError: markers.push is not a function" – Neph Petrichor May 15 '18 at 00:21
  • 4
    `markers = L.layerGroup(markers);` - is the result of `L.layerGroup(markers);` an array? if not, the issue you are having is most certainly to do with the fact that you have asynchrony that you are not dealing with ... i.e. `markers = L.layerGroup(markers);` is executed **before** `function(data) {...}` - currently that line is equivalent to `markers = L.layerGroup(new Array());` – Jaromanda X May 15 '18 at 00:22
  • try swapping the last two lines of code, so you end up with `}); markers = L.layerGroup(markers); });` - of course, then you'll need to know how to deal with further code that relies on `markers` being the final result – Jaromanda X May 15 '18 at 00:24
  • @JaromandaX that was it, and that's what I thought it was... `L.layersGroup([some_array_of_markers])` returns an object (a leaflet layer) so now I need to figure out how to get that out of that function for later use. I'm trying `var some_new_variable = L.layerGroup(markers)` now... – Neph Petrichor May 15 '18 at 00:30
  • no, you can't get an asynchronous result synchronously - if the code is in a function, either use a callback, or return a promise – Jaromanda X May 15 '18 at 01:42
  • 2
    Not directly related to your question, but whatever you're trying to do, it won't work: `d3.json` is **assynchronous**. That means that the line `markers = L.layerGroup(markers);` will run **before** the JSON file is downloaded. You have to deal with the `data` inside the callback. – Gerardo Furtado May 15 '18 at 01:42
  • 1
    @GerardoFurtado - that's directly the problem!! – Jaromanda X May 15 '18 at 01:45
  • see if this code https://pastebin.com/9qz06JDm helps (3 different versions) – Jaromanda X May 15 '18 at 01:51
  • You can use `Object.getPrototypeOf` to check where the variable appears in the `Object's` prototype chain. To check get the type of the variable try doing this: `var arr = [10,20,30]; console.log(Object.getPrototypeOf(arr)["constructor"]["name"]) //Which will print "Array"` – Aditya May 15 '18 at 04:24
  • @AdityaK, there is `Array.isArray(arr)`... – trincot Jan 06 '19 at 21:45

0 Answers0