长话短说,数组的长度取决于所选城市,因此我无法使用索引选择元素,因为我不知道元素将具有什么索引。因此,我需要根据它的一个属性的值来选择它。
在我的例子中,我想选择包含具有属性类型的对象的元素:['locality','政治']
。
示例结果。我没有包括前7个数组元素,因为它们不相关:
7:
address_components: (3) [{…}, {…}, {…}]
types: Array(2)
0: "locality"
1: "political"
8:
address_components: (2) [{…}, {…}]
types: Array(2)
0: "administrative_area_level_1"
1: "political"
如您所见,每个元素都有一个types
属性,它是一个数组。我目前正在尝试使用类型:['locality','political']
而不是索引来选择索引为7的元素。
可悲的是,我不知道如何做到这一点,所以我没有补充任何代码。
[
{
"address_components": [
{
"long_name": "19",
"short_name": "19",
"types": [
"street_number"
]
},
{
"long_name": "Clanbrassil Street Lower",
"short_name": "Clanbrassil Street Lower",
"types": [
"route"
]
},
{
"long_name": "Wood Quay",
"short_name": "Wood Quay",
"types": [
"neighborhood",
"political"
]
},
{
"long_name": "Dublin 8",
"short_name": "Dublin 8",
"types": [
"postal_town"
]
},
{
"long_name": "County Dublin",
"short_name": "County Dublin",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Ireland",
"short_name": "IE",
"types": [
"country",
"political"
]
},
{
"long_name": "D08 X798",
"short_name": "D08 X798",
"types": [
"postal_code"
]
}
],
"formatted_address": "19 Clanbrassil Street Lower, Wood Quay, Dublin, D08 X798, Ireland",
"geometry": {
"location": {
"lat": 53.3349244,
"lng": -6.2740099
},
"location_type": "ROOFTOP",
"viewport": {
"northeast": {
"lat": 53.3362733802915,
"lng": -6.272660919708499
},
"southwest": {
"lat": 53.3335754197085,
"lng": -6.275358880291503
}
}
},
"place_id": "ChIJZckoE7wNZ0gRgHdbQYYvYTk",
"plus_code": {
"compound_code": "8PMG+X9 Dublin, County Dublin, Ireland",
"global_code": "9C5M8PMG+X9"
},
"types": [
"cafe",
"establishment",
"food",
"point_of_interest"
]
},
{
"address_components": [
{
"long_name": "21",
"short_name": "21",
"types": [
"street_number"
]
},
{
"long_name": "R137",
"short_name": "R137",
"types": [
"route"
]
},
{
"long_name": "Wood Quay",
"short_name": "Wood Quay",
"types": [
"neighborhood",
"political"
]
},
{
"long_name": "Dublin 8",
"short_name": "Dublin 8",
"types": [
"postal_town"
]
},
{
"long_name": "County Dublin",
"short_name": "County Dublin",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Ireland",
"short_name": "IE",
"types": [
"country",
"political"
]
}
],
"formatted_address": "21 R137, Wood Quay, Dublin, Ireland",
"geometry": {
"location": {
"lat": 53.3348735,
"lng": -6.273865
},
"location_type": "ROOFTOP",
"viewport": {
"northeast": {
"lat": 53.3362224802915,
"lng": -6.272516019708497
},
"southwest": {
"lat": 53.3335245197085,
"lng": -6.275213980291502
}
}
},
"place_id": "ChIJYVO9ECIMZ0gR2bQrf0Ktmso",
"plus_code": {
"compound_code": "8PMG+WF Dublin, County Dublin, Ireland",
"global_code": "9C5M8PMG+WF"
},
"types": [
"street_address"
]
},
{
"address_components": [
{
"long_name": "18",
"short_name": "18",
"types": [
"street_number"
]
},
{
"long_name": "Clanbrassil Street Lower",
"short_name": "Clanbrassil Street Lower",
"types": [
"route"
]
},
{
"long_name": "Wood Quay",
"short_name": "Wood Quay",
"types": [
"neighborhood",
"political"
]
},
{
"long_name": "Dublin 8",
"short_name": "Dublin 8",
"types": [
"postal_town"
]
},
{
"long_name": "County Dublin",
"short_name": "County Dublin",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Ireland",
"short_name": "IE",
"types": [
"country",
"political"
]
}
],
"formatted_address": "18 Clanbrassil Street Lower, Wood Quay, Dublin, Ireland",
"geometry": {
"location": {
"lat": 53.3349987,
"lng": -6.2739341
},
"location_type": "RANGE_INTERPOLATED",
"viewport": {
"northeast": {
"lat": 53.3363476802915,
"lng": -6.272585119708499
},
"southwest": {
"lat": 53.3336497197085,
"lng": -6.275283080291502
}
}
},
"place_id": "EiwxOCBDbGFuYnJhc3NpbCBTdHJlZXQgTG93ZXIsIER1YmxpbiwgSXJlbGFuZCIaEhgKFAoSCQexJhIiDGdIEUlWW2rVpRfqEBI",
"types": [
"street_address"
]
},
{
"address_components": [
{
"long_name": "Clanbrassil Street Lower",
"short_name": "R137",
"types": [
"route"
]
},
{
"long_name": "Merchants Quay",
"short_name": "Merchants Quay",
"types": [
"neighborhood",
"political"
]
},
{
"long_name": "Dublin 8",
"short_name": "Dublin 8",
"types": [
"postal_town"
]
},
{
"long_name": "County Dublin",
"short_name": "County Dublin",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Ireland",
"short_name": "IE",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Clanbrassil Street Lower, Merchants Quay, Dublin, Ireland",
"geometry": {
"bounds": {
"northeast": {
"lat": 53.33516729999999,
"lng": -6.273907599999999
},
"southwest": {
"lat": 53.3345896,
"lng": -6.274462
}
},
"location": {
"lat": 53.3348778,
"lng": -6.274183000000001
},
"location_type": "GEOMETRIC_CENTER",
"viewport": {
"northeast": {
"lat": 53.33622743029149,
"lng": -6.272835819708497
},
"southwest": {
"lat": 53.33352946970849,
"lng": -6.275533780291502
}
}
},
"place_id": "ChIJzSxnESIMZ0gR8A77SK0ccBQ",
"types": [
"route"
]
},
{
"address_components": [
{
"long_name": "Wood Quay",
"short_name": "Wood Quay",
"types": [
"neighborhood",
"political"
]
},
{
"long_name": "Dublin 8",
"short_name": "Dublin 8",
"types": [
"postal_town"
]
},
{
"long_name": "County Dublin",
"short_name": "County Dublin",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Ireland",
"short_name": "IE",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Wood Quay, Dublin, Ireland",
"geometry": {
"bounds": {
"northeast": {
"lat": 53.3454641,
"lng": -6.2680769
},
"southwest": {
"lat": 53.329641,
"lng": -6.275519999999999
}
},
"location": {
"lat": 53.33275459999999,
"lng": -6.2706881
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 53.3454641,
"lng": -6.2680769
},
"southwest": {
"lat": 53.329641,
"lng": -6.275519999999999
}
}
},
"place_id": "ChIJl78TlSEMZ0gRUHtT8SkTqVA",
"types": [
"neighborhood",
"political"
]
},
{
"address_components": [
{
"long_name": "Dublin 8",
"short_name": "Dublin 8",
"types": [
"postal_town"
]
},
{
"long_name": "County Dublin",
"short_name": "County Dublin",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Ireland",
"short_name": "IE",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Dublin 8, Ireland",
"geometry": {
"bounds": {
"northeast": {
"lat": 53.37132399999999,
"lng": -6.2642699
},
"southwest": {
"lat": 53.325953,
"lng": -6.3563488
}
},
"location": {
"lat": 53.34785249999999,
"lng": -6.318115199999999
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 53.37132399999999,
"lng": -6.2642699
},
"southwest": {
"lat": 53.325953,
"lng": -6.3563488
}
}
},
"place_id": "ChIJLz14IT8MZ0gR2uKxYpAO0Bc",
"types": [
"postal_town"
]
},
{
"address_components": [
{
"long_name": "Dublin City",
"short_name": "Dublin City",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "County Dublin",
"short_name": "County Dublin",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Ireland",
"short_name": "IE",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Dublin City, Co. Dublin, Ireland",
"geometry": {
"bounds": {
"northeast": {
"lat": 53.4111566,
"lng": -6.11309
},
"southwest": {
"lat": 53.2988569,
"lng": -6.387080600000001
}
},
"location": {
"lat": 53.3603142,
"lng": -6.315054200000001
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 53.4111566,
"lng": -6.11309
},
"southwest": {
"lat": 53.2988569,
"lng": -6.387080600000001
}
}
},
"place_id": "ChIJv2RI7foRZ0gRwAKA8azHAAM",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"address_components": [
{
"long_name": "Dublin",
"short_name": "Dublin",
"types": [
"locality",
"political"
]
},
{
"long_name": "County Dublin",
"short_name": "County Dublin",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Ireland",
"short_name": "IE",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Dublin, Ireland",
"geometry": {
"bounds": {
"northeast": {
"lat": 53.42521010000001,
"lng": -6.0439235
},
"southwest": {
"lat": 53.22343009999999,
"lng": -6.4474847
}
},
"location": {
"lat": 53.3498053,
"lng": -6.2603097
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 53.42521010000001,
"lng": -6.0439235
},
"southwest": {
"lat": 53.22343009999999,
"lng": -6.4474847
}
}
},
"place_id": "ChIJL6wn6oAOZ0gRoHExl6nHAAo",
"types": [
"locality",
"political"
]
},
{
"address_components": [
{
"long_name": "County Dublin",
"short_name": "County Dublin",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Ireland",
"short_name": "IE",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Co. Dublin, Ireland",
"geometry": {
"bounds": {
"northeast": {
"lat": 53.6347257,
"lng": -5.9962748
},
"southwest": {
"lat": 53.1781971,
"lng": -6.5468798
}
},
"location": {
"lat": 53.3302033,
"lng": -6.3105655
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 53.6347257,
"lng": -5.9962748
},
"southwest": {
"lat": 53.1781971,
"lng": -6.5468798
}
}
},
"place_id": "ChIJv2RI7foRZ0gRwAKA8azHABg",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"address_components": [
{
"long_name": "Ireland",
"short_name": "IE",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Ireland",
"geometry": {
"bounds": {
"northeast": {
"lat": 55.4602,
"lng": -5.668900100000001
},
"southwest": {
"lat": 51.3066,
"lng": -10.7666001
}
},
"location": {
"lat": 53.41291,
"lng": -8.24389
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 55.4602,
"lng": -5.668900100000001
},
"southwest": {
"lat": 51.3066,
"lng": -10.7666001
}
}
},
"place_id": "ChIJ-ydAXOS6WUgRCPTbzjQSfM8",
"types": [
"country",
"political"
]
}
]
那么,让我们来看看这里发生了什么。
问题:您有一个对象数组,需要查找并返回具有正确类型的数组。
首先,我们需要以某种方式检查数组中的每个对象——for循环可以很好地解决这个问题。具体来说,我们可以将
用于(…of…)
模式:
for(let object of array){
/* do stuff */
}
接下来,我们需要根据设置的条件测试这些对象中的每一个。我们知道我们需要检查每个对象的
类型
属性,因此我们从这里开始:
for(let object of array){
if(someKindOfTest(object.types)){
return object;
}
}
那么,我们实际上如何执行这个测试呢?假设我们知道我们要找的类型。。。
let requiredTypes = ["locality"];
for(let object of array){
if(someKindOfTest(object.types)){
return object;
}
}
...我们可以设计一个测试,只有当对象具有所需的每一种类型时才能通过。听起来像是另一个循环的工作:
let requiredTypes = ["locality"];
for(let object of array){
let hasAllRequiredTypes = true;
// test for each of the required types
for(let requiredType of requiredTypes){
if(!object.types.includes(requiredType)){
// this object doesn't match one of our required types, that disqualifies it
hasAllTypes = false; // flag this object as disqualified
break; // end this loop over the required types
}
}
if(hasAllRequiredTypes) return object;
}
所有这些都可以压缩成一个更简短、更“实用”的版本(@jonas wilms answer就是一个很好的例子),但我认为,在设计这样一个算法时,勾勒出潜在的机制和解决问题的方法是很有用的。
您可以找到包含上述每种类型的元素:
array.find(it => ['locality', 'political'].every(type => it.types.includes(type)))
我需要找到我的集合是否包含相同或超过5个元素的公共变量,并找到这些元素。我这样做: 但是你认为把另一个流放进去过滤是不好的做法。
问题内容: 在XML文档中,我具有共享相同名称的元素,但是属性的值定义了它是什么类型的数据,并且我想从文档中选择所有具有特定值的元素。我是否需要使用XPath(如果可以,您是否可以建议正确的语法)还是有更优雅的解决方案? 这是一些示例XML: 我想选择类型为的所有子代标签的内容。 PS-我正在尝试使用PHP与Netflix API进行接口-这对我的问题无关紧要,但是如果您想提出一种更好的方法,我非
请看do while循环,我认为注释是不言自明的。我想检查该集合是否设置了所有具有符号a、b、c、d的卡片(借助数组或其他任何东西实现它)。
rank ▲ ✰ vote url 54 397 83 538 url 如何知道一个对象有一个特定的属性? 有什么方法可以检测一个对象是否有某些属性?比如: >>> a = SomeClass() >>> a.someProperty = value >>> a.property Traceback (most recent call last): File "<stdin>", line 1
我试图用Vuejs 2实现自定义选择组件。如文档中所述,我不应该直接修改值属性,并建议使用事件将选定的数据传递给父组件。当选项值是一个对象时,我遇到了问题,却得到了[Object object]。 这是我的选择组件模板: 这是脚本部分: 这是父组件 选项: 我期待 但得到了[Object object] 我错过了什么吗?
我正在尝试使用Javascript选择此复选框 下面的代码可以工作,但并不理想 我试过这些 但它们并不起作用。有什么想法能让这件事成功吗? 数据值未硬编码的更新代码