数组和对象是复杂的数据类型,与原始数据类型不同,它们能够同时保存多个值。
您可能会问自己为什么需要两种复杂的数据类型来完成这项任务,并质疑为什么只有一种数据类型不足以完成这项工作。根据您的条件和目标,您可能最好选择使用“对象”来保存“数组”上的多个值,其背后的原因归结为一个原因:可读性。在某些情况下,您最好选择对象而不是数组,反之亦然。
对象更适合,你猜对了,对象!它们能够为其众多值提供名称,并且通常用于描述单个项目附带的属性。数组更适合列表,它们描述其值的能力受到限制,尽管数组在技术上是对象,但由于它们的语法和多个值的方式的独特性,它们赢得了数组的独特名称被存储或访问。您很快就会像我一样理解这些复杂的数据类型,其中对象可以被视为 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() 等。具体选择哪种方法要根据具体情况而定。
//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