1

Trying to work with Mapael Jquery plugin. Plugin need object to draw a map elements.

My PHP code return json encoded array of objects:

[{
    "Aveiro":{
        "latitude":40.6443,
        "longitude":-8.6455,
        "value":10,
        "tooltip":{
            "content":"test"
         }
    }
 },{
    "Lisbon":{
        "latitude":38.7167,
        "longitude":-9.1333,
        "value":10,
        "tooltip":{
            "content":"test"
        }
    }
 },{
    "Entroncamento":{
        "latitude":39.2333,
        "longitude":-9.0833,
        "value":10,
        "tooltip":{
            "content":"test"
        }
    }
 }]   

If I parse this array thru json.parse I will receive object with numeric keys, in my example [0]->[Aveiro],[1]->[Lisbon],[2]->[Entroncamento].

How it possible to have object with keys [Aveiro],[Lisbon],[Entroncamento] without array indexes?

If I return only one object - I receive object with named key and everything works just fine.

2
  • 1
    you have access to edit the php code? if so then share the code which builds that array Commented Jan 8, 2015 at 12:32
  • @metamarket I think you are looking for $array = (array) $yourObject;. That will give you associative array. Commented Jan 8, 2015 at 12:33

1 Answer 1

2

You can reduce the array given by JSON.parse to a single object:

var result = parsed.reduce(function (output, value) {
    return $.extend(output, value);
}, {});

JSFiddle

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.