JS如何根据另一个值访问特定的数组值
JS How to access specific array value based on another value
我正在尝试对数组进行字符串化并获取另一个值等于 EFHK_ATIS:
的特定值
const atisData = JSON.parse(req.responseText);
document.getElementById("metar").innerHTML = JSON.stringify(atisData.atis[??].atis_code);
如果我用数字替换问号,它显然有效,但它 returns 错误元素的 atis_code,因为顺序一直在变化。如何替换问号以导航到呼号等于“EFHK_ATIS”的正确值?
这是 JSON 文件的示例。我想要 atis_code,呼号是 EFHK_ATIS。
"atis": [
{
"cid": xxx,
"name": "Example Name",
"callsign": "ZBAA_ATIS",
"frequency": "199.998",
"facility": 4,
"rating": 2,
"server": "SINGAPORE",
"visual_range": 0,
"atis_code": "F",
"text_atis": [
"ZBAA INFORMATION F, 1100 UTC , DEPARTURE RUNWAY 01,36R,36L,",
"LANDING RUNWAY 01,36L, EXPECT ILS APPROACH, RUNWAY 01,36L, WIND",
"360 DEGREES AT 6 M/S , CAVOK TEMPERATURE 14 DEG C , DEWPOINT -6",
"DEG C , QNH 1030 HPA , ADVISE ON INITIAL CONTACT YOU HAVE",
"INFORMATION F, AND CONFIRM YOU WILL IMPLEMENT RNAV PROCEDURES."
],
"last_updated": "2021-10-15T11:43:52.3645031Z",
"logon_time": "2021-10-15T11:30:24.328892Z"
},
{
"cid": yyy,
"name": "Name Example",
"callsign": "EFHK_ATIS",
"frequency": "135.070",
"facility": 4,
"rating": 4,
"server": "GERMANY",
"visual_range": 0,
"atis_code": "W",
"text_atis": [
"THIS IS HELSINKI-VANTAA ARRIVAL AND DEPARTURE INFORMATION W",
"AT TIME 1120 EXPECT ILS APPROACH ARRIVAL RUNWAY 15",
"DEPARTURE RUNWAY 22R TRANSITION LEVEL 70",
"HEAVY BIRD ACTIVITY IN VICINITY OF HELSINKI-VANTAA WIND 160",
"DEGREES 15 KNOTS VISIBILITY 3000 METERS LIGHT RAIN BROKEN 700",
"FEET TEMPERATURE 9 DEW POINT 9 QNH 994 BECOMING VISIBILITY 10",
"KILOMETERS ADVISE ON INITIAL CONTACT YOU HAVE INFORMATION W"
],
"last_updated": "2021-10-15T11:45:03.875018Z",
"logon_time": "2021-10-15T11:30:27.9814385Z"
},
...
Filter method 就是您要找的
let atis_data = [
{"callsign":"ZBAA_ATIS", "atis_code": "123"},
{"callsign":"ABCD", "atis_code": "abcd"}
]
let filtered = atis_data.filter(data => data["callsign"] == "ZBAA_ATIS")
let result = filtered.map(data => data["atis_code"])
console.log(result)
我正在尝试对数组进行字符串化并获取另一个值等于 EFHK_ATIS:
的特定值const atisData = JSON.parse(req.responseText);
document.getElementById("metar").innerHTML = JSON.stringify(atisData.atis[??].atis_code);
如果我用数字替换问号,它显然有效,但它 returns 错误元素的 atis_code,因为顺序一直在变化。如何替换问号以导航到呼号等于“EFHK_ATIS”的正确值?
这是 JSON 文件的示例。我想要 atis_code,呼号是 EFHK_ATIS。
"atis": [
{
"cid": xxx,
"name": "Example Name",
"callsign": "ZBAA_ATIS",
"frequency": "199.998",
"facility": 4,
"rating": 2,
"server": "SINGAPORE",
"visual_range": 0,
"atis_code": "F",
"text_atis": [
"ZBAA INFORMATION F, 1100 UTC , DEPARTURE RUNWAY 01,36R,36L,",
"LANDING RUNWAY 01,36L, EXPECT ILS APPROACH, RUNWAY 01,36L, WIND",
"360 DEGREES AT 6 M/S , CAVOK TEMPERATURE 14 DEG C , DEWPOINT -6",
"DEG C , QNH 1030 HPA , ADVISE ON INITIAL CONTACT YOU HAVE",
"INFORMATION F, AND CONFIRM YOU WILL IMPLEMENT RNAV PROCEDURES."
],
"last_updated": "2021-10-15T11:43:52.3645031Z",
"logon_time": "2021-10-15T11:30:24.328892Z"
},
{
"cid": yyy,
"name": "Name Example",
"callsign": "EFHK_ATIS",
"frequency": "135.070",
"facility": 4,
"rating": 4,
"server": "GERMANY",
"visual_range": 0,
"atis_code": "W",
"text_atis": [
"THIS IS HELSINKI-VANTAA ARRIVAL AND DEPARTURE INFORMATION W",
"AT TIME 1120 EXPECT ILS APPROACH ARRIVAL RUNWAY 15",
"DEPARTURE RUNWAY 22R TRANSITION LEVEL 70",
"HEAVY BIRD ACTIVITY IN VICINITY OF HELSINKI-VANTAA WIND 160",
"DEGREES 15 KNOTS VISIBILITY 3000 METERS LIGHT RAIN BROKEN 700",
"FEET TEMPERATURE 9 DEW POINT 9 QNH 994 BECOMING VISIBILITY 10",
"KILOMETERS ADVISE ON INITIAL CONTACT YOU HAVE INFORMATION W"
],
"last_updated": "2021-10-15T11:45:03.875018Z",
"logon_time": "2021-10-15T11:30:27.9814385Z"
},
...
Filter method 就是您要找的
let atis_data = [
{"callsign":"ZBAA_ATIS", "atis_code": "123"},
{"callsign":"ABCD", "atis_code": "abcd"}
]
let filtered = atis_data.filter(data => data["callsign"] == "ZBAA_ATIS")
let result = filtered.map(data => data["atis_code"])
console.log(result)