0

I'm trying to compare event.feature.getProperty('township') against timeline.townshipname from my timeline array in my if. Checking for one right now with [0] is fine, but I have a whole column I want to check against. What's the best way to do this?

    //Load Timelines
    var timeline = [];
    jQuery.getJSON(timelines, function(data) {
        var entry = data.feed.entry;
        jQuery(entry).each(function(){
            var townshipname = this.gsx$township.$t;
            var timelinename = this.gsx$timeline.$t;
            var combined = {townshipname, timelinename};
            timeline.push(combined);
        });
    }); 
    // Output from timeline looks like
    // 0: {townshipname: "West Quincy", timelinename: "Ready for drops"}
    // 1: {townshipname: "Woodgate", timelinename: "Ready"}

    //Add infowindow to identify townships
    township_layer.addListener('click', function(event) {
        if (event.feature.getProperty('township') == timeline[0].townshipname){         
            var timepush = timeline[0].timelinename
        } else {
            var timepush = 'No Timeline Entered'
        }
4
  • Loop through the array until you find a match. Break if you find one Commented Apr 8, 2019 at 21:23
  • Hello, I see on the out is says 'townshipname', but you're trying to find the property of 'township'. Is it 'townshipname' or 'township"? Commented Apr 8, 2019 at 21:24
  • 1
    I'm comparing against one that is township (event.feature.geProperty('township') and the other that is townshipname (timeline.townshipname). Two different sources with similar names. Commented Apr 8, 2019 at 21:27
  • @charlietfl, can you give me an example? Commented Apr 8, 2019 at 21:29

2 Answers 2

1

You can create an array of township names from the timeline array of objects, so that you can compare if a specific township is found in your timeline.

This can be done by:

  1. Using Array.prototype.map() to iterate through your timeline array of objects and return a list of all townshipname
  2. Check if a given township is present in your array by using Array.prototype.indexOf()

Example code is as follow:

// Generate an array of townships extract from timeline
var townships = timeline.map(function(item) {
  return item.townshipname;
});

// Attempt to search a given township in your generated array
var townshipIndex = townships.indexOf(event.feature.getProperty('township'));

if (townshipIndex !== -1) {         
    var timepush = timeline[townshipIndex].timelinename;
} else {
    var timepush = 'No Timeline Entered';
}

Alternatively, you can use a for...of loop and break out of it once a match is found. We assume that no timeline is entered as the "ground state", and then we can update that once a match is found:

var timepush = 'No Timeline Entered';
for (var item of timeline) {
  if (item.townshipname === event.feature.getProperty('township')) {
    timepush = item.timelinename;
    break;
  }
}

If you really need IE support, then we can use the classic for loop:

var timepush = 'No Timeline Entered';
for (var i = 0; i < timeline.length; i++) {
  if (timeline[i].townshipname === event.feature.getProperty('township')) {
    timepush = timeline[i].timelinename;
    break;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

So there are a couple of different ways you could this, if you have an indexed array of objects the fastest way would be:

for(var i = 0; i < timeline.length; i++){
    if(event.feature.getProperty('township') == timeline[i].townshipname){
        var timepush = timeline[i].timelinename;
    }
}

I can come up with another example shortly.

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.