我想写一个可以生成随机地址的脚本

时间:2019-01-24 12:56:12

标签: javascript arrays

我想编写一个可以生成随机地址的脚本。 例如:

(34578 Dolphin Street, Wonka NY, 44506).

这是我到目前为止所做的:

var address 

var streetNumber = ['25489', '87459', '35478', '15975', '95125', '78965']

var streetName = ['A street', 'B street', 'C street', 'D street', 'E street', 'F street',]

var cityName = ['Riyadh', 'Dammam', 'Jedda', 'Tabouk', 'Makka', 'Maddena', 'Haiel']

var stateName = ['Qassem State', 'North State', 'East State', 'South State', 'West State']

var zipCode = ['28889', '96459', '35748', '15005', '99625', '71465']

那我现在该怎么办?

3 个答案:

答案 0 :(得分:2)

我建议利用map的{​​{1}}和join函数。映射数组之前,您需要做的就是创建一个接受数组并返回数组随机元素的函数,只需在0和数组大小-1之间选择一个随机索引即可轻松完成此操作。

Array

现在要创建一个随机地址,您只需要在另一个数组中添加所有数组,并使用上面的random函数将function getRandomElement(array) { // Math.random() does not include 1, so this range is from 0 to size - 1 return array[Math.floor(Math.random() * array.length)]; } 添加到其中。这将返回一个随机地址元素数组,您可以在其上使用map函数将数组的所有部分连接到一个字符串中。

join

您还可以在address元素之间添加所需的文本,只需确保按var template = [streetNumber, streetName, cityName, stateName, zipCode]; function getRandomAddress() { return template.map(getRandomElement).join(" "); } 函数的原样返回即可。请查看以下示例:

getRandomAddress
var streetNumber = ["25489", "87459", "35478", "15975", "95125", "78965"];
var streetName = ["A street", "B street", "C street", "D street", "E street", "F street"];
var cityName = ["Riyadh", "Dammam", "Jedda", "Tabouk", "Makka", "Maddena", "Haiel"];
var stateName = ["Qassem State", "North State", "East State", "South State", "West State"];
var zipCode = ["28889", "96459", "35748", "15005", "99625", "71465"];

var template = [streetNumber, " ", streetName, ", ", cityName, " ", stateName, ", ", zipCode];

function getRandomAddress() {
  return template.map(getRandomElement).join("");
}

function getRandomElement(array) {
  if (array instanceof Array) return array[Math.floor(Math.random() * array.length)];
  else return array;
}

document.querySelector("#refresh").addEventListener("click", function() {
  document.querySelector("#output").textContent = getRandomAddress();
});

一些有用的链接:

答案 1 :(得分:0)

尝试生成随机数:Math.floor(Math.random() * (max - min)) + min;

最小值应为0,最大值应为array.lenght

然后您可以执行以下操作:address = streetNumber[myRandNumber(streetNumber)]

然后在myRandNumber函数中根据参数数组的长度计算最大值:)

答案 2 :(得分:0)

您可以尝试使用类似方法迭代数据并添加每个数组之一

var streetNumber = ['25489', '87459', '35478', '15975', '95125', '78965']

var streetName = ['A street', 'B street', 'C street', 'D street', 'E street', 'F street',]

var cityName = ['Riyadh', 'Dammam', 'Jedda', 'Tabouk', 'Makka', 'Maddena', 'Haiel']

var stateName = ['Qassem State', 'North State', 'East State', 'South State', 'West State']

var zipCode = ['28889', '96459', '35748', '15005', '99625', '71465']

function getRandom(input) {
    return input[Math.floor((Math.random() * input.length))];
}

function createAdress() {
    return `${getRandom(streetNumber)} ${getRandom(streetName)} ${getRandom(cityName)} ${getRandom(stateName)} ${getRandom(zipCode)}`;
}

var address = createAdress();
console.log(address);