简体   繁体   English

JavaScript-如何通过查找值来查找对象属性

[英]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: 这是我的data对象的示例:

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 问题:我需要有一个返回true的函数,如果

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. 我希望真正擅长使用JavaScript对象的人可以在这里为我提供帮助。

Update 1 : In short, I need a function that is similar to this: 更新1 :简而言之,我需要一个类似于以下的函数:

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/ 更新2 :这是一个无法正常工作的jsfiddle: http : //jsfiddle.net/p1n20tzk/

Update 3 : Here's a working one (Thanks to @james-k for starting it out) http://jsfiddle.net/edj0qvu0/ 更新3 :这是一个有效的方法(感谢@ james-k使其开始) http://jsfiddle.net/edj0qvu0/

    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;
}

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在数组的每个项目上运行给定的函数,如果该函数对任何一个项目返回true,则返回true。

some function is a prototype of Array object and you must use in arrays. 某些函数是Array对象的原型,必须在数组中使用。 some function has a 3 parameters 某些功能具有3个参数

1.- item - is the current item 2.- index - is the position of the current item 3.- array - array object itself 1.- -是当前项2.- 索引 -当前项目3.- 阵列的位置-数组对象本身

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>

Me again, another iterative function is MAP 再说一次,另一个迭代功能是MAP

MAP function runs every item in array and execute the function for every item. MAP函数运行数组中的每个项目,并为每个项目执行该函数。 replace window.onload function by this. 以此替换window.onload函数。

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");


};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM