陣列和物件是複雜的資料類型,與原始資料類型不同,它們能夠同時保存多個值。
您可能會問自己為什麼需要兩種複雜的資料類型來完成這項任務,並質疑為什麼只有一種資料類型不足以完成這項工作。根據您的條件和目標,您可能最好選擇使用「物件」來保存「陣列」上的多個值,其背後的原因歸結為一個原因:可讀性。在某些情況下,您最好選擇物件而不是數組,反之亦然。
對像比較適合,你猜對了,對象!它們能夠為其眾多值提供名稱,並且通常用於描述單一項目附帶的屬性。數組更適合列表,它們描述其值的能力受到限制,儘管數組在技術上是對象,但由於它們的語法和多個值的方式的獨特性,它們贏得了數組的獨特名稱被存儲或訪問。您很快就會像我一樣理解這些複雜的資料類型,其中物件可以被視為 3 維,數組可以被視為 2 維。
-3D 物件與 2D 陣列
//AN OBJECT let person = { voice: "soft", age: "32" }; //AN ARRAY let groceryList = ['bananas', 'coconuts', 'grapes']
-Above we have an example of an object doing what it does best, describing a 3 dimensional object in reality. Here we have the initialization of the variable 'animal' using the 'let' keyword to point to an object; which contains it's information within curly braces '{}'. Within the object are 'key: value' pairs. Keys are to the left of ':', and their values are to the right, with each pair separated by ','. As you can see with an object, we can give each value it holds a unique name to help describe and identify the value it points to. The age of the person is 32, and their voice is soft. You may notice that this format is easily readable and comes natural to understand, even someone who has no clue what coding is will likely be able to glance at those lines of code, and get a general understanding of what is going on.
在此之下,我們有一系列美麗的購物清單中最典型的物品,並且可以找到相同的自然可讀性。請注意,該數組由括號“[]”表示。
物件與陣列存取:
console.log(dog.name) //returns "Fifo" console.log(groceryList[0] //returns bananas
As mentioned earlier, objects are 3-dimensional, and arrays are 2-dimensional. The first way this becomes noticeable is when you try to access the values of an array or object. In a 2-dimensional plane, the surroundings are described with coordinates; a series of numbers that equate to the description of a particular location. This is how arrays behave, their coordinates are called indexes, and their particular location is a value. Like coordinates, indexes will always be numbers, and arrays cannot access their values in any other way unless you pass in a number next to it surrounded by brackets '[#]'. Even the brackets themselves move like a 2 dimensional object; up, down, left, right, there are no curves to help one describe the complexities of a 3-dimensional plane, then comes Objects. Objects access their values with their 'key'. Earlier, the "key: value" pair was '"voice: "soft"', thus we can reference the dogs name by typing "person.voice". Just like 3-dimensional objects in our non-virtual reality, the properties of these objects are described with words, given names so-to-speak. The phenomenological conclusion we draw for what these properties are in relation to the object we experience, equates to the value we give to that word.
哲學與理解對象:我們可以將質地描述為柔軟,將氣味描述為難聞,將情感描述為痛苦,但所有概念最終都依賴於兩個詞來描述。當描述現實中的物體時,僅“軟”這個詞可能會被誤解並且難以理解。如果只是說“'人'就是'軟'”,每個人的觀念可能會得出不同的結論;一個人可能認為你說“軟人”是善良和有愛心的,另一個人可能會說“軟人」是軟弱和軟弱的。然而,如果我們說“一個'人'有一個'軟'的'質地'”,或者“一個'人'有一個'軟'的'聲音',那麼我們最終會得出一個不太分歧的結論:這就是為什麼「一個'物件'有一個'鍵',它是一個'值'」可以理解為3 維的。
物件和陣列操作
物件和陣列可以透過不同的方式進行操作。數組是透過索引號存取的,而對於對象,它們的值是使用稱為「鍵」的東西來存取的。由於每個鍵都有命名,因此在物件中導航比在陣列中導航更困難。這就是為什麼陣列更適合編號列表,而物件更適合描述單個項目的屬性。
用於刪除和加入陣列的方法有 .pop()、.push()、.shift()、.unshift()、.splice() 等。具體選擇哪種方法要根據具體情況而定。
//向數組和物件新增/刪除值
person.name = "山姆"; //將鍵「name」加到值為「sam」的person
person["sign"] = "pisces" //將 iykyk 金鑰加入 a
array.push(tomato) //將tomato加到陣列末尾
array.unshift(cherries) //在開頭加上-1
array.splice(1, 2, 'hello world') //從索引 1 開始,刪除 2 個索引並在索引 1 處插入 hello world。
// 5
array.pop() //刪除最後一個索引
array.shift() //刪除陣列中的第一個索引
刪除animal.sign //刪除動物的關鍵標誌
array.slice(1) //從陣列的副本中刪除第一個元素
//adding / removing values to arrays and objects person.name = "Sam"; //adds key 'name' to person with value of "sam" person["sign"] = "pisces" //adds key iykyk to a array.push(tomato) //adds tomato to the end of array array.unshift(cherries) //adds -1 to beginning array.splice(1, 2, 'hello world') //starts at index 1, removes 2 indexes and inserts hello world at index 1. // 5 array.pop() //removes last index array.shift() //removes first index in array delete animal.sign //removes key sign from animal array.slice(1) //removes first element from a COPY of the array
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3