0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Javascript tutorial  Array編

Posted at

download.png

JavaScript Arrayオブジェクトが持つmethod

下記のコードを実行すると、methodの一覧を確認することができます。

index.js
    console.log(Array.prototype);

image.png

この中から、いくつかピックアップして紹介します。

index.js
    const shopping = ["bread", "milk", "cheese", "hummus", "noodles"];
    
    // Accessing and modifying array items
    console.log(shopping[0]);
    // Modify an item in an array
    shopping[0] = "tahini";
    
    // Note that an array inside an array
    const random = ["tree", 759, [0, 1, 2]];
    console.log(random[2][2]);
    
    // Finding the index of items in array
    const birds = ["Parrot", "Falcom", "Owl"];
    console.log(birds.indexOf("Owl"));
    console.log(birds.indexOf("Rabbit"));
    
    // Adding items
    const cities = ["Manchester", "Liverpool"];
    cities.push("Cardiff");
    console.log(cities);
    cities.push("Bradford", "Brighton");
    console.log(cities);
    const newLength = cities.push("Bristol");
    console.log(newLength);
    
    // To add an item to the start of the array
    cities.unshift("Edinburgh");
    console.log(cities);
    
    // Removing items
    // To remove the last item from the array
    cities.pop();
    console.log(cities);
    
    // To remove the first item from an array
    cities.shift();
    console.log(cities);
    
    // To remove an item you know the index
    const index = cities.indexOf("Liverpool");
    if (index !== -1) {
      cities.splice(index, 2);
    }
    console.log(cities);
    
    // Accessing every item
    for (const city of cities) {
      console.log(city);
    }
    
    function double(number) {
      return number * 2;
    }
    const numbers = [5, 2, 7, 6];
    const doubled = numbers.map(double);
    console.log(doubled);
    
    // To create a new array containing only the items in the original array that match some test
    function isLong(citi) {
      return citi.length > 8;
    }
    const longer = cities.filter(isLong);
    console.log(longer);
    
    // Converting between strings and arrays
    const data = "Manchester,London,Liverpool,Birmingham,Leeds,Carlisle";
    
    // Split it at each comma
    const newCities = data.split(",");
    console.log("Length:" + newCities.length, newCities);
    
    // The opposite way
    const oppositeCityes = newCities.join(",");
    console.log(oppositeCityes);
    
    // Another way of converting an array to a string
    const dogNames = ["Rocket", "Flash", "Bella", "Slugger"];
    console.log(dogNames.toString());
    
    // A valid array index
    console.log(Object.keys(cities));    

参考資料

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?