1

I would like to use a each function in my script but this function is used in an object which means it returns an error... The object I want to use each in is newPlots.

Maybe you know a trick?

I want to add plots in newPlots by using the content returned by AJAX. In plain text, newPlots look like this :

                var newPlots = {
                    "City" : {
                        x : 45.834444,
                        y : 1.261667,
                        text : {content : "City"},
                        tooltip : {content : "City"}
                    }
                    , "City2" : {
                        size:60,
                        x : 47.323056,
                        y : 5.041944,
                        text : {
                            content : "City",
                            position : "left",
                            margin : 5
                        }
                    }
                }

And the thing I want to do is using each function in newPlots.

Here you can find my code :

$.ajax({
type: "post",
url: '<?php echo $this->Url->build(array('controller'=>'Villes','action'=>'viewresult')); ?>',
data: {
    departements: dptcode
},
dataType : "json",
success: function (ret) {
           // Update some plots and areas attributes ...
           var updatedOptions = {'plots' : {}};
           // add some new plots ..
           var newPlots = {

               $.each(ret, function(index, element) {
                       "Limoge" : {
                           x : element.x,
                           y : element.y,
                           text : {content : "Limoge"},
                           tooltip : {content : "Limoge"}
                       },
               });



           }

           // and delete some others ...
           var deletedPlots = [];

           $(".mapcontainer").trigger('update', [updatedOptions, newPlots, deletedPlots, {animDuration : 1000}]);




},
error : function(xhr, textStatus, errorThrown) {
    alert('An error occurred! ' + errorThrown);
}
});
6
  • It doesn't work... Commented Apr 8, 2016 at 12:25
  • 2
    So bascically, you want to get an array of objects??? Your code doesn't make sense and because you don't explain what is your expected behaviour, it is hard to figure it out... Commented Apr 8, 2016 at 12:25
  • Change it to --> var newPlots = $.map(ret, function(index, element) { return "Limoge" : { x : element.x, y : element.y, text : {content : "Limoge"}, tooltip : {content : "Limoge"} } }); Commented Apr 8, 2016 at 12:35
  • Thank you for your help. Firebug tells me : SyntaxError: missing ; before statement "Limoge" : { Commented Apr 8, 2016 at 12:42
  • Are you trying to add an object entry with key "Limoge" multiple times? That does not sound like it makes much sense. Commented Apr 8, 2016 at 12:55

1 Answer 1

1

The way you try to extend newPlots is not syntactically correct.

Try doing

           var newPlots = newPlots || { };
           $.each(ret, function(index, element) {
                    newPlots[element.name] = {
                       x : element.x,
                       y : element.y,
                       text : {content : element.name}, 
                       tooltip : {content : element.name} 
                   };
           });
Sign up to request clarification or add additional context in comments.

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.