首页 > 其他分享 >解锁数组操作的艺术:你的终极方法集指南(鸿蒙篇)

解锁数组操作的艺术:你的终极方法集指南(鸿蒙篇)

时间:2024-05-31 23:32:51浏览次数:13  
标签:console log 鸿蒙 解锁 Expected output Array 终极 const

数组在编程中扮演着重要的角色,它们允许我们存储和操作一系列的元素。以下是对数组操作方法的全面介绍:

  • Array.concat()

  1. concat() 方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。
    const array1 = ['a', 'b', 'c'];
    const array2 = ['d', 'e', 'f'];
    const array3 = array1.concat(array2);
    
    console.log(array3);
  • Array.copyWithin()

  1. copyWithin() 方法浅复制数组的一部分到同一数组中的另一个位置,并返回它,不会改变原数组的长度。
    const array1 = ['a', 'b', 'c', 'd', 'e'];
    
    // Copy to index 0 the element at index 3
    console.log(array1.copyWithin(0, 3, 4));
    // Expected output: Array ["d", "b", "c", "d", "e"]
    
    // Copy to index 1 all elements from index 3 to the end
    console.log(array1.copyWithin(1, 3));
    // Expected output: Array ["d", "d", "e", "d", "e"]
  • Array.entries()

  1. entries() 方法返回一个新的数组迭代器对象,该对象包含数组中每个索引的键/值对。
    const array1 = ['a', 'b', 'c'];
    
    const iterator1 = array1.entries();
    
    console.log(iterator1.next().value);
    // Expected output: Array [0, "a"]
    
    console.log(iterator1.next().value);
    // Expected output: Array [1, "b"]
    
  • Array.every()

  1. every() 方法测试一个数组内的所有元素是否都能通过指定函数的测试。它返回一个布尔值。
    const isBelowThreshold = (currentValue) => currentValue < 40;
    
    const array1 = [1, 30, 39, 29, 10, 13];
    
    console.log(array1.every(isBelowThreshold));
    // Expected output: true
    
  • Array.fill()

  1. fill() 方法用一个固定值填充一个数组中从起始索引(默认为 0)到终止索引(默认为 array.length)内的全部元素。它返回修改后的数组。
    const array1 = [1, 2, 3, 4];
    
    // Fill with 0 from position 2 until position 4
    console.log(array1.fill(0, 2, 4));
    // Expected output: Array [1, 2, 0, 0]
    
    // Fill with 5 from position 1
    console.log(array1.fill(5, 1));
    // Expected output: Array [1, 5, 5, 5]
    
    console.log(array1.fill(6));
    // Expected output: Array [6, 6, 6, 6]
  • Array.filter()

  1. filter() 方法创建给定数组一部分的浅拷贝,其包含通过所提供函数实现的测试的所有元素。
    const words = ['spray', 'elite', 'exuberant', 'destruction', 'present'];
    
    const result = words.filter((word) => word.length > 6);
    
    console.log(result);
    // Expected output: Array ["exuberant", "destruction", "present"]
  • Array.find()

  1. find() 方法返回数组中满足提供的测试函数的第一个元素的值。否则返回 undefined
    const array1 = [5, 12, 8, 130, 44];
    
    const found = array1.find((element) => element > 10);
    
    console.log(found);
    // Expected output: 12
  • Array.findIndex()

  1. findIndex() 方法返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回 -1。
    const array1 = [5, 12, 8, 130, 44];
    
    const isLargeNumber = (element) => element > 13;
    
    console.log(array1.findIndex(isLargeNumber));
  • Array.flat()

  1. flat() 方法创建一个新的数组,并根据指定深度递归地将所有子数组元素拼接到新的数组中。
    const arr1 = [0, 1, 2, [3, 4]];
    
    console.log(arr1.flat());
    // expected output: Array [0, 1, 2, 3, 4]
    
    const arr2 = [0, 1, [2, [3, [4, 5]]]];
    
    console.log(arr2.flat());
    // expected output: Array [0, 1, 2, Array [3, Array [4, 5]]]
    
    console.log(arr2.flat(2));
    // expected output: Array [0, 1, 2, 3, Array [4, 5]]
    
    console.log(arr2.flat(Infinity));
    // expected output: Array [0, 1, 2, 3, 4, 5]
  • Array.flatMap()

  1. flatMap() 方法对数组中的每个元素应用给定的回调函数,然后将结果展开一级,返回一个新数组。
    const arr1 = [1, 2, 1];
    
    const result = arr1.flatMap((num) => (num === 2 ? [2, 2] : 1));
    
    console.log(result);
    // Expected output: Array [1, 2, 2, 1]
  • Array.forEach()

  1. forEach() 方法对数组的每个元素执行一次给定的函数。
    const array1 = ['a', 'b', 'c'];
    
    array1.forEach((element) => console.log(element));
  • Array.includes()

  1. includes() 方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回 false
  • const array1 = [1, 2, 3];
    
    console.log(array1.includes(2));
    // Expected output: true
    
    const pets = ['cat', 'dog', 'bat'];
    
    console.log(pets.includes('cat'));
    // Expected output: true
    
    console.log(pets.includes('at'));
    // Expected output: false
    

    Array.indexOf()

  1. indexOf() 方法返回数组中第一次出现给定元素的下标,如果不存在则返回 -1。
    const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
    
    console.log(beasts.indexOf('bison'));
    // Expected output: 1
    
    // Start from index 2
    console.log(beasts.indexOf('bison', 2));
    // Expected output: 4
    
    console.log(beasts.indexOf('giraffe'));
    // Expected output: -1
  • Array.join()

  1. join() 方法将一个数组的所有元素连接成一个字符串并返回这个字符串,用逗号或指定的分隔符字符串分隔。如果数组只有一个元素,那么将返回该元素而不使用分隔符。
    const elements = ['Fire', 'Air', 'Water'];
    
    console.log(elements.join());
    // Expected output: "Fire,Air,Water"
    
    console.log(elements.join(''));
    // Expected output: "FireAirWater"
    
    console.log(elements.join('-'));
    // Expected output: "Fire-Air-Water"
    
  • Array.keys()

  1. keys() 方法返回一个新的数组迭代器对象,其中包含数组中每个索引的键。
    
    for (const key of iterator) {
      console.log(key);
    }
    
    // Expected output: 0
    // Expected output: 1
    // Expected output: 2

  • Array.lastIndexOf()

  1. lastIndexOf() 方法返回数组中给定元素最后一次出现的索引,如果不存在则返回 -1。该方法从 fromIndex 开始向前搜索数组。
    const animals = ['Dodo', 'Tiger', 'Penguin', 'Dodo'];
    
    console.log(animals.lastIndexOf('Dodo'));
    // Expected output: 3
    
    console.log(animals.lastIndexOf('Tiger'));
    // Expected output: 1
  • Array.map()

  1. map() 方法创建一个新数组,这个新数组由原数组中的每个元素都调用一次提供的函数后的返回值组成。
    const array1 = [1, 4, 9, 16];
    
    // Pass a function to map
    const map1 = array1.map((x) => x * 2);
    
    console.log(map1);
    // Expected output: Array [2, 8, 18, 32]
  • Array.pop()

  1. pop() 方法从数组中删除最后一个元素,并返回该元素的值。此方法会更改数组的长度。
    const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
    
    console.log(plants.pop());
    // Expected output: "tomato"
    
    console.log(plants);
    // Expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]
    
    plants.pop();
    
    console.log(plants);
    // Expected output: Array ["broccoli", "cauliflower", "cabbage"]
  • Array.push()

  1. push() 方法将指定的元素添加到数组的末尾,并返回新的数组长度。
    const animals = ['pigs', 'goats', 'sheep'];
    
    const count = animals.push('cows');
    console.log(count);
    // Expected output: 4
    console.log(animals);
    // Expected output: Array ["pigs", "goats", "sheep", "cows"]
    
    animals.push('chickens', 'cats', 'dogs');
    console.log(animals);
    // Expected output: Array ["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]
    
  • Array.reduce()

  1. reduce() 方法对数组中的每个元素按序执行一个提供的 reducer 函数,每一次运行 reducer 会将先前元素的计算结果作为参数传入,最后将其结果汇总为单个返回值。
  2. 第一次执行回调函数时,不存在“上一次的计算结果”。如果需要回调函数从数组索引为 0 的元素开始执行,则需要传递初始值。否则,数组索引为 0 的元素将被用作初始值,迭代器将从第二个元素开始执行(即从索引为 1 而不是 0 的位置开始)。
    const array1 = [1, 2, 3, 4];
    
    // 0 + 1 + 2 + 3 + 4
    const initialValue = 0;
    const sumWithInitial = array1.reduce(
      (accumulator, currentValue) => accumulator + currentValue,
      initialValue,
    );
    
    console.log(sumWithInitial);
    // Expected output: 10
  • Array.reduceRight()

  1. reduceRight() 方法对累加器(accumulator)和数组的每个值(按从右到左的顺序)应用一个函数,并使其成为单个值。
    const array1 = [
      [0, 1],
      [2, 3],
      [4, 5],
    ];
    
    const result = array1.reduceRight((accumulator, currentValue) =>
      accumulator.concat(currentValue),
    );
    
    console.log(result);
    // Expected output: Array [4, 5, 2, 3, 0, 1]
  • Array.reverse()

  1. reverse() 方法就地反转数组中的元素,并返回同一数组的引用。数组的第一个元素会变成最后一个,数组的最后一个元素变成第一个。换句话说,数组中的元素顺序将被翻转,变为与之前相反的方向。
    const array1 = ['one', 'two', 'three'];
    console.log('array1:', array1);
    // Expected output: "array1:" Array ["one", "two", "three"]
    
    const reversed = array1.reverse();
    console.log('reversed:', reversed);
    // Expected output: "reversed:" Array ["three", "two", "one"]
    
    // Careful: reverse is destructive -- it changes the original array.
    console.log('array1:', array1);
    // Expected output: "array1:" Array ["three", "two", "one"]
  • Array.shift()

  1. shift() 方法从数组中删除第一个元素,并返回该元素的值。此方法更改数组的长度。
    const array1 = [1, 2, 3];
    
    const firstElement = array1.shift();
    
    console.log(array1);
    // Expected output: Array [2, 3]
    
    console.log(firstElement);
    // Expected output: 1
  • Array.slice()

  1. slice() 方法返回一个新的数组对象,这一对象是一个由 start 和 end 决定的原数组的浅拷贝(包括 start,不包括 end),其中 start 和 end 代表了数组元素的索引。原始数组不会被改变。
    const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
    
    console.log(animals.slice(2));
    // Expected output: Array ["camel", "duck", "elephant"]
    
    console.log(animals.slice(2, 4));
    // Expected output: Array ["camel", "duck"]
    
    console.log(animals.slice(1, 5));
    // Expected output: Array ["bison", "camel", "duck", "elephant"]
    
    console.log(animals.slice(-2));
    // Expected output: Array ["duck", "elephant"]
    
    console.log(animals.slice(2, -1));
    // Expected output: Array ["camel", "duck"]
    
    console.log(animals.slice());
    // Expected output: Array ["ant", "bison", "camel", "duck", "elephant"]
  • Array.some()

  1. const array = [1, 2, 3, 4, 5];
    
    // Checks whether an element is even
    const even = (element) => element % 2 === 0;
    
    console.log(array.some(even));
    // Expected output: true
    
  • Array.sort()

  1. sort() 方法就地对数组的元素进行排序,并返回对相同数组的引用。默认排序是将元素转换为字符串,然后按照它们的 UTF-16 码元值升序排序。
  2. 由于它取决于具体实现,因此无法保证排序的时间和空间复杂度。
    const months = ['March', 'Jan', 'Feb', 'Dec'];
    months.sort();
    console.log(months);
    // Expected output: Array ["Dec", "Feb", "Jan", "March"]
    
    const array1 = [1, 30, 4, 21, 100000];
    array1.sort();
    console.log(array1);
    // Expected output: Array [1, 100000, 21, 30, 4]
    
  • Array.splice()

  1. splice() 方法就地移除或者替换已存在的元素和/或添加新的元素。
    const months = ['Jan', 'March', 'April', 'June'];
    months.splice(1, 0, 'Feb');
    // Inserts at index 1
    console.log(months);
    // Expected output: Array ["Jan", "Feb", "March", "April", "June"]
    
    months.splice(4, 1, 'May');
    // Replaces 1 element at index 4
    console.log(months);
    // Expected output: Array ["Jan", "Feb", "March", "April", "May"]
  • Array.toLocaleString()

  1. toLocaleString() 方法返回一个字符串,表示数组中的所有元素。每个元素通过调用它们自己的 toLocaleString 方法转换为字符串,并且使用特定于语言环境的字符串(例如逗号“,”)分隔开。
    const array1 = [1, 'a', new Date('21 Dec 1997 14:12:00 UTC')];
    const localeString = array1.toLocaleString('en', { timeZone: 'UTC' });
    
    console.log(localeString);
    // Expected output: "1,a,12/21/1997, 2:12:00 PM",
    // This assumes "en" locale and UTC timezone - your results may vary
  • Array.toString()

  1. toString() 方法返回一个字符串,表示指定的数组及其元素。
    const array1 = [1, 2, 'a', '1a'];
    
    console.log(array1.toString());
    // Expected output: "1,2,a,1a"
  • Array.unshift()

  1. unshift() 方法将指定元素添加到数组的开头,并返回数组的新长度。
    const array1 = [1, 2, 3];
    
    console.log(array1.unshift(4, 5));
    // Expected output: 5
    
    console.log(array1);
    // Expected output: Array [4, 5, 1, 2, 3]
  • Array.values()

  1. values() 方法返回一个新的数组迭代器对象,该对象迭代数组中每个元素的值。
    const array1 = ['a', 'b', 'c'];
    const iterator = array1.values();
    
    for (const value of iterator) {
      console.log(value);
    }
    
    // Expected output: "a"
    // Expected output: "b"
    // Expected output: "c"
    

标签:console,log,鸿蒙,解锁,Expected,output,Array,终极,const
From: https://blog.csdn.net/m0_46109348/article/details/139361742

相关文章

  • AI大模型终极指南:零基础到精通,一篇文章全掌握!
    前言在这个AI大模型的时代,知识的获取和分享变得前所未有地重要。作为一名互联网老兵,我深感有责任将我所掌握的AI大模型知识分享给大家。无论你是初学者还是已经有一定基础的开发者,我都希望这份资料能帮助你更好地理解和应用AI大模型。一、大模型全套的学习路线学习大型人......
  • 鸿蒙HarmonyOS实战-Web组件(Cookie及数据存储)
    ......
  • 鸿蒙开发的知识点(ArkTS阶段)(一)
    1、ArkTS常见的数据类型有哪些?        ①为什么要有数据类型        例子:在烹饪中,我们需要知道食材的种类(如蔬菜、肉类等)和数量(如1个苹果、200克鸡肉等)。如果我们不区分这些食材的种类和数量,那么在做菜时就会遇到问题。例如,如 果我们将所有食材都......
  • Oracle查询锁、解锁
    参考:https://blog.csdn.net/haiross/article/details/486533331.查询数据库中的锁select*fromv$lock;select*fromv$lockwhereblock=1;2.查询被锁的对象select*fromv$locked_object;3.查询阻塞查被阻塞的会话select*fromv$lockwherelmode=0andtypein('......
  • 【最新鸿蒙应用开发】——优化之从启动和响应速度作为思路
    优化--提升应用启动速度和响应速度从应用程序启动和运行过程的思路来思考,比如想办法提升冷启动速度,使用并行化(多线程并发,异步并发)、预加载、缓存等方法手段,提升系统资源利用率,减少主线程负载,加快应用的响应速度。以下是一些详细细节的总结。1、提升应用冷启动速度应用启动......
  • 鸿蒙HarmonyOS实战-Web组件(页面跳转和浏览记录)
    ......
  • TYUT移动框架技术(鸿蒙开发)复习提纲
    好多...一点一点写主要是整理一些上课说到的概念,大概看一下,只可能出选择填空简答什么的,老师发的那个认证题库最好也看一下,我感觉大概率会出那里的题。代码什么的可以看这个这个 ,我感觉还是挺不错的。考试前一周我会再出一个代码的。第一章HarmonyOS设计理念一次开发,......
  • 高效日志管理:通过 Systemd Journal 收集日志的终极指南
    随着systemd成了主流的init系统,systemd的功能也在不断的增加,比如对系统日志的管理。Systemd设计的日志系统好处多多,这里笔者就不再赘述了,本文笔者主要介绍systemdjournal收集日志的三种方式:程序使用libc库中的syslog()函数输出的日志使用printf()函数打印的......
  • 两个跨域页面进行跳转传参的终极方案
    本文约定:A页面:跳转前的原来页面,假设为a.comB页面:将要跳转的目标页面,假设为b.com一、简单方案说到页面跳转,首先想到的就是用a标签://在A页面点击链接,并将参数data传到B页面<ahref="http://b.com?data=1"target="_blank"/>//在B页面接收A页面传过来的参数<script>va......
  • 安卓再见!华为纯血鸿蒙截图流出,鸿蒙操作系统时代已经来临!6 月 21 日见!
    5 月 25 日上午,开放原子开源基金会 OpenHarmony 开发者大会 2024 于深圳正式举行,会上余承东表示,华为在2020年就将HarmonyOS的基础能力贡献给了开放原子开源基金会,截至目前,已有2000多名开发者支持OpenHarmony社区发展,累计贡献核心代码6200多万行。华为表示,Op......