Search code examples
javascriptjsondojo

.map() is not a function Error on Mapping Dynamically created Object


I am trying to map a dynamically created Object to an Array like this

          params.junctionBarriers = dojo.toJson(junctionBarriers);
          // printing the object in the console (Please look at attached image)
          console.log(params.junctionBarriers);

          var value = [];
          var data = params.junctionBarriers.map(function (s) {
             value.push(webMercatorUtils.xyToLngLat(s.x, s.y, true));
              return s;
          })

but I am getting this error

params.junctionBarriers.map is not a function

enter image description here

As you can see from the image the params.junctionBarriers is dynamically loaded but the .map() is not valid. Why is this, and how I can fix it?


Solution

  • Look at the documentation for dojo.toJson

    That method converts an object to a string in JSON format.

    A String does not have the method map.

    So why don't you simply do junctionBarriers.map? given you already have the array of objects in that variable (which is NOT params.junctionBarriers)