当前位置:  首页>> 技术小册>> 编程入门课:Javascript从入门到实战

JavaScript中,对象和数组都是非常重要的数据结构,可以用来存储和操作各种数据。本文将介绍JavaScript中对象和数组的概念和作用,并提供一些相关的代码示例。

对象
在JavaScript中,对象是一个包含属性和方法的集合。可以将对象看作是一个键值对的集合,其中每个键都是唯一的,并且每个键都对应一个值。

创建对象
可以使用以下方式创建对象:

字面量语法

  1. const person = {
  2. name: 'John',
  3. age: 30,
  4. email: 'john@example.com'
  5. };

构造函数语法

  1. function Person(name, age, email) {
  2. this.name = name;
  3. this.age = age;
  4. this.email = email;
  5. }
  6. const person = new Person('John', 30, 'john@example.com');

访问对象属性
可以使用以下方式访问对象属性:

点语法

  1. console.log(person.name); // 'John'
  2. console.log(person.age); // 30
  3. console.log(person.email); // 'john@example.com'

方括号语法

  1. console.log(person['name']); // 'John'
  2. console.log(person['age']); // 30
  3. console.log(person['email']); // 'john@example.com'

修改对象属性
可以使用以下方式修改对象属性:

  1. person.name = 'Jane';
  2. person.age = 35;
  3. person.email = 'jane@example.com';
  4. console.log(person.name); // 'Jane'
  5. console.log(person.age); // 35
  6. console.log(person.email); // 'jane@example.com'

对象方法
可以将函数添加到对象中作为方法:

  1. const person = {
  2. name: 'John',
  3. age: 30,
  4. email: 'john@example.com',
  5. greet: function() {
  6. console.log(`Hello, my name is ${this.name}.`);
  7. }
  8. };
  9. person.greet(); // 'Hello, my name is John.'

数组
在JavaScript中,数组是一种特殊的对象,用于存储有序的数据集合。可以将数组看作是一个由多个元素组成的列表,其中每个元素都有一个数字索引,表示其在数组中的位置。

创建数组
可以使用以下方式创建数组:

字面量语法

  1. const fruits = ['apple', 'banana', 'orange'];
  2. 构造函数语法
  3. javascript
  4. Copy code
  5. const fruits = new Array('apple', 'banana', 'orange');

访问数组元素
可以使用以下方式访问数组元素:

  1. console.log(fruits[0]); // 'apple'
  2. console.log(fruits[1]); // 'banana'
  3. console.log(fruits[2]); // 'orange'

修改数组元素
可以使用以下方式修改数组元素:

  1. fruits[0] = 'pear';
  2. fruits[1] = 'grape';
  3. fruits[2] = 'kiwi';
  4. console.log(fruits[0]); // 'pear'
  5. console.log(fruits[1]); // 'grape'
  6. console.log(fruits[2]); // 'kiwi'

数组方法
JavaScript提供了许多方法来操作数组。以下是一些常见的数组方法:

push(): 将一个或多个元素添加到数组的末尾,并返回新数组的长度。

  1. const fruits = ['apple', 'banana'];
  2. const length = fruits.push('orange', 'kiwi');
  3. console.log(fruits); // ['apple', 'banana', 'orange', 'kiwi']
  4. console.log(length); // 4

pop(): 移除数组的最后一个元素,并返回该元素的值。

  1. const fruits = ['apple', 'banana', 'orange', 'kiwi'];
  2. const lastFruit = fruits.pop();
  3. console.log(fruits); // ['apple', 'banana', 'orange']
  4. console.log(lastFruit); // 'kiwi'

shift(): 移除数组的第一个元素,并返回该元素的值。

  1. const fruits = ['apple', 'banana', 'orange'];
  2. const firstFruit = fruits.shift();
  3. console.log(fruits); // ['banana', 'orange']
  4. console.log(firstFruit); // 'apple'

unshift(): 将一个或多个元素添加到数组的开头,并返回新数组的长度。

  1. const fruits = ['banana', 'orange'];
  2. const length = fruits.unshift('apple', 'kiwi');
  3. console.log(fruits); // ['apple', 'kiwi', 'banana', 'orange']
  4. console.log(length); // 4

slice(): 返回数组的一个片段,不会改变原数组。

  1. const fruits = ['apple', 'banana', 'orange', 'kiwi'];
  2. const slicedFruits = fruits.slice(1, 3);
  3. console.log(slicedFruits); // ['banana', 'orange']
  4. console.log(fruits); // ['apple', 'banana', 'orange', 'kiwi']

splice(): 从数组中添加或删除元素,并返回被删除的元素。

  1. const fruits = ['apple', 'banana', 'orange', 'kiwi'];
  2. const removedFruits = fruits.splice(1, 2, 'pear', 'grape');
  3. console.log(fruits); // ['apple', 'pear', 'grape', 'kiwi']
  4. console.log(removedFruits); // ['banana', 'orange']

concat(): 将多个数组合并为一个新数组。

  1. const fruits1 = ['apple', 'banana'];
  2. const fruits2 = ['orange', 'kiwi'];
  3. const allFruits = fruits1.concat(fruits2);
  4. console.log(allFruits); // ['apple', 'banana', 'orange', 'kiwi']

forEach(): 对数组中的每个元素执行一次指定的函数。

  1. const fruits = ['apple', 'banana', 'orange', 'kiwi'];
  2. fruits.forEach(function(fruit) {
  3. console.log(fruit);
  4. });
  5. // 'apple'
  6. // 'banana'
  7. // 'orange'
  8. // 'kiwi'

以上只是一些常见的数组方法,JavaScript还有许多其他有用的数组方法可供使用。


该分类下的相关小册推荐: