我有一个包含属性和子属性的对象,我需要找到它们以查找它们是否包含某个值。
以下是我的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'
}
},
问题:如果
,我需要一个返回true的函数location1 == 'Port Canaveral' && location2 == 'Orlando'
,如果
,则为false(location1 == 'Orlando' && location2 == 'Orlando') || (location1 == 'Port Canaveral' && location2 == 'Port Canaveral')
但我只知道应该确定相应位置的“值”属性。我希望那些擅长使用JavaScript对象的人可以帮助我。
更新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')));
}
更新2 :这是一个不起作用的jsfiddle:http://jsfiddle.net/p1n20tzk/
更新3 :这是一个有效的工作(感谢@ james-k启动它)http://jsfiddle.net/edj0qvu0/
答案 0 :(得分:1)
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;
}
答案 1 :(得分:1)
您好尝试使用迭代器功能一些
array.some(function(item,index,array){return item == 'something';})
some 在数组中的每个项目上运行给定函数,如果函数对任何一个项目返回true,则返回true。
某些函数是Array对象的原型,您必须在数组中使用。 某些功能有3个参数
1 .- 项目 - 是当前项目 2 .- 索引 - 是当前项目的位置 3 .- 数组 - 数组对象本身
检查并尝试!
<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>
答案 2 :(得分:0)
我再次,另一个迭代函数是MAP
MAP函数运行数组中的每个项目并为每个项目执行函数。由此替换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");
};