创建数组
任何类型的对象,都可以放入数组中。
var shopping = ['bread', 'milk', 'cheese', 'hummus', 'noodles'];
shopping;
// (5) ["bread", "milk", "cheese", "hummus", "noodles"]
var sequence = [1, 1, 2, 3, 5, 8, 13];
var random = ['tree', 795, [0, 1, 2]];
存取、变更,数组元素
shopping[0];
// returns "bread"
shopping[0] = 'tahini';
shopping;
// shopping will now return [ "tahini", "milk", "cheese", "hummus", "noodles" ]
获取数组长度
sequence.length;
// should return 7
var sequence = [1, 1, 2, 3, 5, 8, 13];
for (var i = 0; i < sequence.length; i++) {
console.log(sequence[i]);
}
部分数组方法
字符串和数组之间的转换
var myData = 'Manchester,London,Liverpool,Birmingham,Leeds,Carlisle';
var myArray = myData.split(',');
myArray;
myArray.length;
myArray[0]; // the first item in the array
myArray[1]; // the second item in the array
myArray[myArray.length-1]; // the last item in the array
var myNewString = myArray.join(',');
myNewString;
var dogNames = ['Rocket','Flash','Bella','Slugger'];
dogNames.toString(); //Rocket,Flash,Bella,Slugger
添加、移除,数组元素
var myArray = ['Manchester', 'London', 'Liverpool', 'Birmingham', 'Leeds', 'Carlisle'];
// 向数组末尾添加元素
myArray.push('Cardiff');
myArray;
myArray.push('Bradford', 'Brighton');
myArray;
var newLength = myArray.push('Bristol');
myArray;
newLength;
// 移除数组末尾的元素
myArray.pop();
var removedItem = myArray.pop();
myArray;
removedItem;
// 向数组起首添加元素
myArray.unshift('Edinburgh');
myArray;
// 移除数组起首元素
var removedItem = myArray.shift();
myArray;
removedItem;
let customName = document.getElementById('customname');
let randomize = document.querySelector('.randomize');
let story = document.querySelector('.story');
function randomValueFromArray(array) {
return array[Math.floor(Math.random() * array.length)];
}
let storyText = "It was 94 fahrenheit outside, so :insertx: went for a walk. When they got to :inserty:, they stared in horror for a few moments, then :insertz:. Bob saw the whole thing, but was not surprised — :insertx: weighs 300 pounds, and it was a hot day.";
let insertX = ["Willy the Goblin", "Big Daddy", "Father Christmas"];
let insertY = ["the soup kitchen", "Disneyland", "the White House"];
let insertZ = ["spontaneously combusted", "melted into a puddle on the sidewalk", "turned into a slug and crawled away"];
randomize.addEventListener("click", result);
function result() {
let newStory = storyText;
let xItem = randomValueFromArray(insertX);
let yItem = randomValueFromArray(insertY);
let zItem = randomValueFromArray(insertZ);
newStory = newStory.replace(":insertx:", xItem);
newStory = newStory.replace(":inserty:", yItem);
newStory = newStory.replace(":insertz:", zItem);
if (customName.value !== '') {
let name = customName.value;
newStory = newStory.replace("Bob", name);
}
if (document.getElementById("uk").checked) {
let weight = Math.round(300 * 0.0714286) + " stone";
let temperature = Math.round((94 - 32) * 5 / 9) + " centigrade";
newStory = newStory.replace("300 pounds", weight);
newStory = newStory.replace("94 fahrenheit", temperature);
}
story.textContent = newStory;
story.style.visibility = 'visible';
}