美文网首页
JavaScript 中的数组

JavaScript 中的数组

作者: 蜗牛是不是牛 | 来源:发表于2022-08-28 14:41 被阅读0次

    什么是数组?

    数组是一个特定的变量,它可以保存多个值:

    const fruits = ["Apple", "Banana", "Orange"];
    
    

    为什么是数组?

    由于 Array 可以存储 n 个值,因此很容易存储和访问数据。

    例如:-假设我们必须存储水果的数据,所以我们有两个选择。

    选项 1:- 为 n 个fruit创建 n 个变量

    let fruit1 = "Apple";
    let fruit2 = "Banana";
    let fruit3 = "Orange";
    
    

    选项 2:- 创建一个数组并在其中存储 n 个fruit。

    const fruits = ["Apple", "Banana", "Orange"];
    
    

    创建一个数组

    复制

    COPY

    const fruits = ["Apple", "Banana", "Orange"];
    
    

    使用关键字的另一种方式new

    const fruits = new Array("Apple", "Banana", "Orange");
    
    

    访问数组的元素

    要访问数组元素,您可以使用它index number默认情况index number0

    const fruits = ["Apple", "Banana", "Orange"];
    let fruit = fruits[0];
    
    

    改变数组的元素

    fruits[0] = "PineApple";
    
    

    另一种方式

    const fruits = ["Apple", "Banana", "Orange"];
    fruits[0] = "PineApple";
    
    

    访问数组的所有元素

    复制

    COPY

    const fruits = ["Apple", "Banana", "Orange"];
    document.getElementById("demo").innerHTML = fruits;
    
    

    数组方法

    .length()

    const fruits = [
         "Apple", 
         "Banana", 
         "Orange",
         "Pineapple"
      ];
     
    console.log(fruits.length); 
    
    // output:-
    // 4
    

    .pop()

    pop方法用于移除数组的最后一个元素

    const fruits = [
        "Apple", 
        "Banana", 
        "Orange",
        "Pineapple"
    ];
    
    let removefruit = fruits.pop()
    
    
    console.log(`${removefruit}  removed from the array`);
    console.log(fruits);
    
    // output
    
    // "Pineapple  removed from the array"
    // [object Array] (3)
    // ["Apple","Banana","Orange"]
    

    .push()

    push()方法用于在数组末尾添加元素

    const fruits = [
        "Apple", 
        "Banana", 
        "Orange",
        "Pineapple"
    ];
    
    console.log(fruits);
    
    fruits.push("Watermelon")
    
    console.log(fruits);
    
    // output
    
    // ["Apple","Banana","Orange","Pineapple"]
    // ["Apple","Banana","Orange","Pineapple","Watermelon"]
    

    .concat

    .concat用于合并两个数组的值

    const fruits = [
        "Apple", 
        "Banana", 
        "Orange",
        "Pineapple"
    ];
    const vegetables = [
        "Carrots",
        "Potatoes",
        "Chilli"
    ];
    
    let vegetablesAndFruits = vegetables.concat(fruits)
    
    console.table(vegetablesAndFruits);
    
    // output:-
    // ["Carrots","Potatoes","Chilli","Apple","Banana","Orange","Pineapple"]
    

    .indexOf()

    .indexOf用于查找index number给定数组元素的

    const fruits = [
        "Apple", 
        "Banana", 
        "Orange",
        "Pineapple"
    ];
    
    console.log(fruits.indexOf("Orange"));
    
    // output:-
    // 2
    

    .lastIndexOf()

    lastIndexOf()用于查找项目最后一次重复的时间

    let travelHistory = ["Rome", "India", "Russia", "Nepal", "Dubai", "India"];
    
    console.log(travelHistory.lastIndexOf("Rome")); // 0
    console.log(travelHistory.lastIndexOf("India")); // 5
    console.log(travelHistory.lastIndexOf("Russia")); // 2
    console.log(travelHistory.lastIndexOf("canada")); // -1
    

    .join()

    const fruits = ["apple", "banana", "orange"]
    
    let fruitsStr = fruits.join(" & ")
    console.log(fruitsStr);
    
    // Output:-
    // "apple & banana & orange"
    

    .reverse()

    .reverse()方法用于以相反的顺序定位数组的元素

    const fruits = ["apple", "banana", "orange"]
    
    let fruit = fruits.reverse()
    console.log(fruit);
    
    // output:-
    // ["orange","banana","apple"]
    

    .sort()

    const fruits = ["banana","pineapple", "orange", "apple"]
    
    
    fruits.sort()
    console.log(fruits);
    
    // output:-
    // ["apple","banana","orange","pineapple"]
    

    .toString()

    const fruits = ["banana","pineapple", "orange", "apple"]
    
    
    console.log(fruits.toString());
    
    
    // output:-
    // "banana,pineapple,orange,apple"
    

    .copyWithin()

    const fruits = ["banana","pineapple", "orange", "apple"]
    
    
    // (target,start,end)
    // end option
    fruits.copyWithin(0, 2, 3)
    console.log(fruits);
    
    // output:-
    // ["orange","pineapple","orange","apple"]
    
    
    Resources
    

    文章来源:https://themohitgupta.hashnode.dev/array-in-javascript

    相关文章

      网友评论

          本文标题:JavaScript 中的数组

          本文链接:https://www.haomeiwen.com/subject/mhahnrtx.html