17个实用的Javascript数组对象方法转载

原创
小哥 3年前 (2022-10-26) 阅读数 178 #js教程
文章标签 JavaScript

使用这些数组和对象方法,您将不需要再次使用它们。 forwhile 循环。

.filter()

根据数组的条目是否由特定条件创建来创建新数组。

举例

创建一组符合法定饮酒年龄的学生年龄。

const studentsAge = [17, 16, 18, 19, 21, 17];
const ableToDrink = studentsAge.filter( age => age > 18 );
// ableToDrink will be equal to [19, 21]

.map()

通过操作另一个数组中的值来创建新数组。 它是数据操作的理想之选,经常使用React因为它是一个不变的方法。

举例

创建一个数组并在每个数字的开头添加一个数组。 $

const numbers = [2, 3, 4, 5];
const dollars = numbers.map( number => $ + number);
// dollars will be equal to [$2, $3, $4, $5]

.reduce()

这种经常被忽视的方法使用累加器将数组中的所有项减少到单个值。 非常适合计算总数。 返回值可以是任何类型(即对象、数组、字符串、整数)。

举例

向数组中添加一个整数。

const numbers = [5, 10, 15];
const total = numbers.reduce( (accumulator, currentValue) => accumulator + currentValue);
// total will be equal to 30

在MDN在文档中列出 .reduce() 有一些很好的用例提供了如何执行操作的示例,例如平铺数组、通过属性对对象进行分组以及从数组中删除重复项。

.forEach()

将函数应用于数组中的每一项。

举例

将每个阵列项目记录到控制台。

const emotions = [happy, sad, angry];
emotions.forEach( emotion => console.log(emotion) );
// Will log the following:
// happy
// sad
// angry

.some()

检查是否有符合条件的项。 一个很好的用例是检查用户权限。 它可以类似地使用 .forEach() ,您可以对每个数组项执行操作,并在返回真值后跳出循环。

举例

检查是否有 admin

const userPrivileges = [user, user, user, admin];
const containsAdmin = userPrivileges.some( element => element === admin);
// containsAdmin will be equal to true

.every()

.some() 类似,但请检查数组中的所有项是否都符合条件。

举例

检查所有评级是否等于或大于等于3星。

const ratings = [3, 5, 4, 3, 5];
const goodOverallRating = ratings.every( rating => rating >= 3 );
// goodOverallRating will be equal to true

.includes()

检查数组是否包含值。 类似于 .some() ,但不是查找传递的条件,而是查看数组是否包含特定值。

举例

检查数组是否包含字符串。 waldo 的项目。

const names = [sophie, george, waldo, stephen, henry];
const includesWaldo = names.includes(waldo);
// includesWaldo will be equal to true

Array.from()

这是一种基于数组或字符串创建数组的静态方法。 还可以将 map 回调函数作为参数传递给新数组中的数据。 老实说,我不知道为什么会有人通过考试。 .map() 方法来使用它。

举例

从字符串创建数组。

const newArray = Array.from(hello);
// newArray will be equal to [h, e, l, l, o]

创建一个数组,该数组的值是其他数组中每一项的两倍。

const doubledValues = Array.from([2, 4, 6], number => number * 2);
// doubleValues will be equal to [4, 8, 12]

Object.values()

返回对象值的数组。

举例

const icecreamColors = {
    chocolate: brown,
    vanilla: white,
    strawberry: red,
}

const colors = Object.values(icecreamColors);
// colors will be equal to ["brown", "white", "red"]

Object.keys()

返回对象的键的数组。

举例

const icecreamColors = {
  chocolate: brown,
  vanilla: white,
  strawberry: red,
}

const types = Object.keys(icecreamColors);
// types will be equal to ["chocolate", "vanilla", "strawberry"]

Object.entries()

创建包含对象的密钥。/值对数组。

举例

const weather = {
  rain: 0,
  temperature: 24,
  humidity: 33,
}

const entries = Object.entries(weather);
// entries will be equal to
// [[rain, 0], [temperature, 24], [humidity, 33]]

Array spread

使用扩展运算符 (...) 扩展数组允许扩展数组中的元素。 当将一串数组连接在一起时,它很有用。 从数组中删除某些元素时,请避免使用DIRECT。 splice() 方法,因为它可以关联 slice() 方法组合使用以防止数组中的直接变化。

举例

合并两个数组。

const spreadableOne = [1, 2, 3, 4];
const spreadableTwo = [5, 6, 7, 8];

const combined = [...spreadableOne, ...spreadableTwo];
// combined will be equal to [1, 2, 3, 4, 5, 6, 7, 8]

删除数组元素,而不更改原始数组。

const animals = [squirrel, bear, deer, salmon, rat];
const mammals = [...animals.slice(0,3), ...animals.slice(4)];
// mammals will be equal to [squirrel, bear, deer, rat]

Object spread

扩展对象允许您向没有突变的对象添加新的属性和值(即创建新对象),或者您可以将多个对象组合在一起。应该注意的是,传播的对象不是为了复制而嵌套的。

举例

添加新的对象属性和值,而不更改原始对象。

const spreadableObject = {
  name: Robert,
  phone: iPhone
};

const newObject = {
  ...spreadableObject,
  carModel: Volkswagen
}
// newObject will be equal to
// { carModel: Volkswagen, name: Robert, phone: iPhone }

Function Rest

函数可以使用剩余参数语法将任意数量的参数作为数组接受。

举例

显示传递的参数数组。

function displayArgumentsArray(...theArguments) {
  console.log(theArguments);
}

displayArgumentsArray(hi, there, bud);
// Will print [hi, there, bud]

Object.freeze()

防止修改现有对象特性或向对象添加新特性和值。 一般认为 const 会做的, const 只允许修改一个对象。

举例

冻结对象以防止更改 name 属性。

const frozenObject = {
  name: Robert
}

Object.freeze(frozenObject);

frozenObject.name = Henry;
// frozenObject will be equal to { name: Robert }

Object.seal()

停止向对象添加任何新属性,但仍允许更改现有属性。

举例

密封对象以防止添加 wearsWatch 属性。

const sealedObject = {
  name: Robert
}

Object.seal(sealedObject);

sealedObject.name = Bob;
sealedObject.wearsWatch = true;
// sealedObject will be equal to { name: Bob }

Object.assign()

允许将对象分组在一起。 也可以使用此方法,因为可以改用对象扩展语法。 与对象扩展运算符一样, Object.assign() 也不会进行深度克隆。

举例

将两个对象合并为一个。

const firstObject = {
  firstName: Robert
}

const secondObject = {
  lastName: Cooper
}

const combinedObject = Object.assign(firstObject, secondObject);
// combinedObject will be equal to { firstName: Robert, lastName: Cooper }
版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除

热门