1. 程式人生 > >JavaScript - First step - Arrays

JavaScript - First step - Arrays

建立陣列

任何型別的物件,都可以放入陣列中。

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';
}