0

I currently have an array as structured below:

var locations = [
{
    lat: 54.603928, //latitude 
    lon: -5.915033, //longditude
    title: 'Odyssey Arena, Belfast', //name of venue
    html: [ //the HTML that appears inside the map tooltips
        '<h4>Odyssey Arena, Belfast.</h4>', //name of venue
        '<p>23rd March 2015</p>' //date of venue
    ].join(''), //Joins the HTML 
    icon: '//maps.google.com/mapfiles/markerA.png', //the marker for the location on the google map
    zoom: 7//makes the map zoom in closer to the location when clicked
},
{
    lat: 53.347496,
    lon: -6.228508,
    title: 'O2 Arena, Dublin',
    html: [
        '<h4>CO2 Arena, Dublin.</h4>',
        '<p>20th March 2015</p>'
    ].join(''),
    icon: '//maps.google.com/mapfiles/markerB.png',
    zoom: 7
}];

I need to extract the title value from each and place it in a new array.

3 Answers 3

2

You can use the .map() method:

var newArray = $.map(locations, function(item){
  return item.title;
});
Sign up to request clarification or add additional context in comments.

Comments

1

This is not an associative array, but just an array of objects. You also don't need jQuery.

locations.map(function(l){return l.title})

Here, i use ES5 Array.map() to get the title property of each array item (not supported IE8 and under).

1 Comment

Thanks, I noticed after it wasn't an associate array so I quickly edited the title.
0
for ( var i = 0; i < locations.length; i++ ) {
    console.log (locations[i].title);
}

You can also use for...in statement for iterating through objects.

1 Comment

Sorry! I didn't notice the "placing into array" part... :(

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.