Daniel Harris
Daniel Harris

Reputation: 1905

JavaScript - How to find an object property by finding value

I have an object with properties and sub-properties which I need to locate to find if they contain a certain value.

Here is a sample of my data object:

var data = [
{ 
    value: 'Orlando International Airport (MCO)', 
    data: { 
            category: 'Airport',
            address: '1 Jeff Fuqua Blvd., Orlando, FL',
            airport: 'MCO',
            location: 'Orlando'
          } 
},
{ 
    value: 'Orlando Sanford International Airport (SFB)', 
    data: { 
            category: 'Airport',
            address: '1200 Red Cleveland Blvd., Sanford, FL',
            airport: 'SFB',
            location: 'Orlando'
          } 
},
{ 
    value: 'Port Canaveral Cruise Terminal', 
    data: { 
            category: 'Cruise Terminal',
            address: 'Port Canaveral, FL',
            airport: '',
            location: 'Port Canaveral'
          } 
},
{ 
    value: 'Baymont Inn & Suites Florida Mall/Orlando', 
    data: { 
            category: 'Hotel',
            address: '8820 S Orange Blossom Trail, Orlando, FL',
            airport: '',
            location: 'Orlando'
          } 
},

The problem: I need to have a function that returns true if

location1 == 'Port Canaveral' && location2 == 'Orlando'

and false if

(location1 == 'Orlando' && location2 == 'Orlando') || (location1 == 'Port Canaveral' && location2 == 'Port Canaveral')

But I only know the "value" properties which should determine the corresponding locations. I hope someone who is really good at using JavaScript objects can help me here.

Update 1: In short, I need a function that is similar to this:

function locations_different(string1, string2) {
    i1 = data.value.indexOf(string1);
    location1 = data[i1].data.location;

    i2 = data.value.indexOf(string2);
    location2 = data[i2].data.location;

    return ((location1 == 'Port Canaveral' && location2 == 'Orlando') && !((location1 == 'Orlando' && location2 == 'Orlando') || (location1 == 'Port Canaveral' && location2 == 'Port Canaveral')));
}

Update 2: Here's a non-working jsfiddle: http://jsfiddle.net/p1n20tzk/

Update 3: Here's a working one (Thanks to @james-k for starting it out) http://jsfiddle.net/edj0qvu0/

Upvotes: 0

Views: 111

Answers (3)

James K
James K

Reputation: 56

Me again, another iterative function is MAP

MAP function runs every item in array and execute the function for every item. replace window.onload function by this.

window.onload = function() {
var arrFound = ['Port Canaveral', 'Orlando'];
    data.map
            (
                function(item, index, array) {
                    var j;
                    var curLocation = item.data.location;
                    if (arrFound.length > 0) {//check if are more items in arrFound array
                        if (curLocation == 'Port Canaveral' || curLocation == 'Orlando') {//Validate condition
                            j = arrFound.indexOf(curLocation);
                            if (j != -1)//if exists 
                                arrFound.splice(j, 1); //delete from arrFound array
                        }
                    }                                        
                }
            );

    if (arrFound.length == 0)
        alert("The 'Port Canaveral' And 'Orlando' Locations are in the same array");
    else
        alert("The 'Port Canaveral' And 'Orlando' Locations aren't in the same array");


};

Upvotes: 0

James K
James K

Reputation: 56

Hello try with iterator function some

array.some(function(item,index,array){return item == 'something';})

some Runs the given function on every item in the array and returns true if the function returns true for any one item.

some function is a prototype of Array object and you must use in arrays. some function has a 3 parameters

1.-item - is the current item 2.-index - is the position of the current item 3.-array - array object itself

Check and try!

<script type="text/javascript">
    var i;       
    var data = [
                { 
                    value: 'Orlando International Airport (MCO)', 
                    data: { 
                            category: 'Airport',
                            address: '1 Jeff Fuqua Blvd., Orlando, FL',
                            airport: 'MCO',
                            location: 'Orlando'
                          } 
                },
                { 
                    value: 'Orlando Sanford International Airport (SFB)', 
                    data: { 
                            category: 'Airport',
                            address: '1200 Red Cleveland Blvd., Sanford, FL',
                            airport: 'SFB',
                            location: 'Orlando'
                          } 
                },
                { 
                    value: 'Port Canaveral Cruise Terminal', 
                    data: { 
                            category: 'Cruise Terminal',
                            address: 'Port Canaveral, FL',
                            airport: '',
                            location: 'Port Canaveral'
                          } 
                },
                { 
                    value: 'Baymont Inn & Suites Florida Mall/Orlando', 
                    data: { 
                            category: 'Hotel',
                            address: '8820 S Orange Blossom Trail, Orlando, FL',
                            airport: '',
                            location: 'Orlando'
                          }}];

window.onload = function() {
    var vlocation = 'Orlando';//Condition 1
    var vcategory = 'Hotel';//Condition 2
    if (data.some(function(item, index, array) { i = index; return item.data.location == vlocation && item.data.category == vcategory; }))
        alert("Found At Position " + i + "\n" + "Category:" + data[i].data.category + "\n" + "Address:" + data[i].data.address + "\n" + "AirPort:" + data[i].data.airport + "\n" + "Location:" + data[i].data.location);
    else
        alert("No matches!");
};
</script>

Upvotes: 1

Brandon Berger
Brandon Berger

Reputation: 5

    Object.byString = function(o, s) {
    s = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
    s = s.replace(/^\./, '');           // strip a leading dot
    var a = s.split('.');
    for (var i = 0, n = a.length; i < n; ++i) {
        var k = a[i];
        if (k in o) {
            o = o[k];
        } else {
            return;
        }
    }
    return o;

if(Object.byString(data, 'valueString.location') == 'Port Canaveral'){
   return true;
}else{
   return false;
}

Upvotes: 1

Related Questions