JavaScript Array 属性、方法 (二)

JavaScript Array 属性、方法 (一)javascript

every()

方法用于检测数组全部元素是否都符合指定条件(经过函数提供)java

若收到一个空数组,此方法在一切状况下都会返回 true数组

  • 语法
array.every(function(currentValue,index,arr), thisValue) 复制代码
  • 参数app

    • function
      • currentValue: 必须 当前元素的值
      • index :可选 当前元素的索引值
      • arr: 可选。当前元素属于的数组对象
    • thisValue: 可选。对象做为该执行回调时使用,传递给函数,用做 "this" 的值。若是省略了 thisValue ,"this" 的值为 "undefined"
  • 返回值:函数

    布尔值。若是全部元素都经过检测返回 true,不然返回 false。post

  • Example测试

function isBelowThreshold(currentValue) {
  return currentValue < 40;
}
let array1 = [1, 30, 39, 29, 10, 13];

console.log(array1.every(isBelowThreshold));
// output: true
复制代码

fill()

用一个固定值填充一个数组中从起始索引到终止索引内的所有元素。不包括终止索引。ui

  • 语法
array.fill(value[, start[, end]])
复制代码
  • 参数this

    • value: 用来填充数组元素的值。spa

    • start 可选 起始索引,默认值为0。

    • end 可选 终止索引,默认值为 this.length

  • 返回值

    修改后的数组

let array1 = [1, 2, 3, 4];
// 将数组array1中索引为(2 - 4)元素的值修改成0
console.log(array1.fill(0, 2, 4));
// output[Array]: [1, 2, 0, 0]

// 将数组array1索引从1开始到最大索引的元素的值修改成5
console.log(array1.fill(5, 1));
// output[Array]: [1, 5, 5, 5]

// 将数组array1中的元素的值修改成6
console.log(array1.fill(6));
// output[Array]: [6, 6, 6, 6]
复制代码

filter()

建立一个新的数组,新数组中的元素是经过检查指定数组中符合条件的全部元素。

  • 语法
array.filter(function(currentValue,index,arr), thisValue) 复制代码
  • 参数

    • function
      • currentValue: 必须。当前元素的值
      • index: 可选。当前元素的索引值
      • arr: 可选。当前元素属于的数组对象
    • thisValue: 可选。对象做为该执行回调时使用,传递给函数,用做 "this" 的值。若是省略了 thisValue ,"this" 的值为 "undefined"
  • 返回值

返回数组,包含了符合条件的全部元素。若是没有符合条件的元素则返回空数组。

let words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
// 返回 words 中元素字符长度大于6的元素
const result = words.filter(word => word.length > 6);
console.log(result);
// output[Array]: ["exuberant", "destruction", "present"]
复制代码
let fruits = [
  {
    name: 'apple',
    price: '13'
  },
  {
    name: 'banana',
    price: '8'
  },{
    name: 'cherry',
    price: '20'
  }
];
// 查找单价 大于10的水果
const result = fruits.filter(fruit => fruit.price > 10);
console.log(result);
// output[Array]: [{{name: "apple", price: "13"},{name: "cherry", price: "20"}]
复制代码

find()

返回数组中知足条件的第一个元素的值。不然返回 undefined

  • 语法
array.find(function(currentValue, index, arr),thisValue) 复制代码
  • 参数

    • function
      • currentValue: 必须。当前元素的值
      • index: 可选。当前元素的索引值
      • arr: 可选。当前元素属于的数组对象
    • thisValue: 可选。 传递给函数的值通常用 "this" 值。若是这个参数为空, "undefined" 会传递给 "this" 值
  • 返回值

返回符合测试条件的第一个数组元素值,若是没有符合条件的则返回 undefined

let array1 = [5, 12, 8, 130, 44];

let found = array1.find(function(element) {
  return element > 10;
});
console.log(found);
// output[Array]: 12
复制代码

findIndex()

方法返回传入一个测试条件(函数)符合条件的数组第一个元素位置

  • 语法
array.findIndex(function(currentValue, index, arr), thisValue) 复制代码
  • 参数

    • function
      • currentValue: 必须。当前元素的值
      • index: 可选。当前元素的索引值
      • arr: 可选。当前元素属于的数组对象
    • thisValue: 可选。 传递给函数的值通常用 "this" 值。若是这个参数为空, "undefined" 会传递给 "this" 值
  • 返回值

返回符合测试条件的第一个数组元素索引,若是没有符合条件的则返回 -1

let array1 = [5, 12, 8, 130, 44];

function isLargeNumber(element) {
  return element > 13;
}

console.log(array1.findIndex(isLargeNumber));
// output: 3
复制代码

flat()

按照一个可指定的深度递归遍历数组,将全部元素与遍历到的子数组中的元素合并为一个新数组返回。

  • 语法
let newArray = arr.flat([depth])
复制代码
  • 参数

    • depth: 指定要提取嵌套数组的结构深度,默认值为 1。
  • 返回值

一个包含将数组与子数组中全部元素的新数组。

// 扁平化嵌套数组
let arr1 = [1, 2, [3, 4]];
console.log(arr1.flat()); 
// output[Array]: [1, 2, 3, 4]

let arr2 = [1, 2, [3, 4, [5, 6]]];
console.log(arr2.flat());
// output[Array]: [1, 2, 3, 4, [5, 6]]

let arr3 = [1, 2, [3, 4, [5, 6]]];
console.log(arr3.flat(2));
// output[Array]: [1, 2, 3, 4, 5, 6]

//使用 Infinity 做为深度,展开任意深度的嵌套数组
let arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
console.log(arr4.flat(Infinity));
// output[Array]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
复制代码

flat() 方法会移除数组中的空项:

var arr5 = [1, 2, , 4, 5];
console.log(arr5.flat());
// output[Array]: [1, 2, 4, 5]
复制代码

flat()替代方案

flat()替代方案

相关文章
相关标签/搜索