我正在制作Google Maps多标记地图,然后单击标记,我想在自定义弹出窗口中获取有关位置的信息,我遇到了变量问题,即在哪里放置了位置信息。我想从数组中获取一个位置信息,然后将其附加到我的弹出窗口中。我搜索了很多帖子,但没有找到解决方法
这是我的JS。
for i in range(len(message)-4):
code = message[i:i+5]
答案 0 :(得分:0)
您可以使用函数find
并检查第三个索引值。
此方法对获取第三个索引值([_, __, eventId])
的每个数组进行解构。
let events = [ [ 56.92436, 24.11869, '1234', 'Location 1' ], [ 58.780083, 24.256746, '5678', 'Location 2' ], [ 56.95042, 24.10352, '9012', 'Location 3' ]];
$('button').click(getInfo);
function getInfo() {
let id = "5678";
let event = events.find(([_, __, eventId]) => eventId === id);
$("#info").html('');
$("#info").append(event);
}
body { padding: 20px; font-family: Helvetica;}#banner-message { background: #fff; border-radius: 4px; padding: 20px; font-size: 25px; text-align: center; transition: all 0.2s; margin: 0 auto; width: 300px;}button { background: #0084ff; border: none; border-radius: 5px; padding: 8px 14px; font-size: 15px; color: #fff;}#banner-message.alt button { background: #fff; color: #000;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div id="info">Info about location</div><button>Get info about Location 2</button>
没有解构逻辑
let events = [ [ 56.92436, 24.11869, '1234', 'Location 1' ], [ 58.780083, 24.256746, '5678', 'Location 2' ], [ 56.95042, 24.10352, '9012', 'Location 3' ]];
$('button').click(getInfo);
function getInfo() {
let id = "5678";
let event = events.find(arr => {
let eventId = arr[2];
return eventId === id;
});
$("#info").html('');
$("#info").append(event);
}
body { padding: 20px; font-family: Helvetica;}#banner-message { background: #fff; border-radius: 4px; padding: 20px; font-size: 25px; text-align: center; transition: all 0.2s; margin: 0 auto; width: 300px;}button { background: #0084ff; border: none; border-radius: 5px; padding: 8px 14px; font-size: 15px; color: #fff;}#banner-message.alt button { background: #fff; color: #000;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div id="info">Info about location</div><button>Get info about Location 2</button>