簡體   English   中英

JavaScript 中的變量超過 1 個 URL

[英]Variable more than 1 URL in JavaScript

我想在 JavaScript 中設置 1 個以上的 url 變量。

我正在准備一張地圖,其中只添加了 1 個 url。 我想要超過 1 個。

我嘗試使用這樣的代碼:

var url = 'Peterborough.json';
var url = 'test.json'; 

但不幸的是,只有第二個在工作。 第一個看起來像是關閉了。

有誰知道如何在 1 行中放置 1 個以上的 url,以使它們都正常工作?

謝謝

一個變量在某一時刻只能有一個值,那么使用數組來代替如何:

var url = ['Peterborough.json', 'text.json'];

console.log(url[0]);  // =>  'Peterborough.json'
console.log(url[1]);  // =>  'text.json'

不能聲明兩個同名的變量。 您在這里有兩個選擇:

1- 只需重命名您的變量之一,例如:

var url = 'Peterborough.json';
var url2 = 'test.json'; 

2- 使用數組:

var urls = ["Peterborough.json", "test.json"];
//here urls[0] will be "Peterborough.json" (the first element of the array)
//and urls[1] will be "test.json"  (the second element of the array)

你可以這樣做:

var url0 = 'Peterborough.json';
var url1 = 'test.json'; 
console.log(url0);
console.log(url1);

我希望它有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM