search
HomeWeb Front-endJS TutorialDetailed explanation of new feature examples of array extension

I feel that a lot of practical features have been added to the expansion of arrays, and they feel quite important

1. Array.of() converts data variables into Array form

{
  let arr = Array.of(3,4,7,9,11);
  console.log('arr=',arr);//[3,4,7,9,11]

  let empty=Array.of();//[]
  console.log('empty',empty);
}

  2. Array.from() is used to convert two types of objects into real arrays: Array-like objects (array-like object) and traversable (iterable) objects (including ES6’s new data structures Set and Map)

At the same time Array. from can also accept a second parameter, which is similar to the map method of an array. It is used to process each element and put the processed value into the returned array.

{
  let p=document.querySelectorAll('p');
  let pArr=Array.from(p);
  pArr.forEach(function(item){
    console.log(item.textContent);
  });

  console.log(Array.from([1,3,5],function(item){return item*2}));//[2,6,10]
}

 3. fill(data,startIndex,endIndex-1) fills the array. If there is only one data parameter, all will be replaced. If there are three parameters Replace all startIndex~endIndex-1 with data

{
  console.log('fill-7',[1,'a',undefined].fill(7));//[7,7,7]
  console.log('fill,pos',['a','b','c'].fill(7,1,3));
}

 4. keys (return all array subscripts) values ​​(return all values ​​of the array) entries (Including all keys and values) 

{  for(let index of ['1','c','ks'].keys()){
    console.log('keys',index);
  }  for(let value of ['1','c','ks'].values()){
    console.log('values',value);
  }  for(let [index,value] of ['1','c','ks'].entries()){
    console.log('values',index,value);
  }
}

  5. copyWithin(p1,p2,p3) From The p1 position starts to be overwritten with the data from p2 to p3-1

{
  console.log([1,2,3,4,5].copyWithin(0,3,4));//[4,2,3,4,5]
}

 6. find() stops when it finds the first value that meets the conditions and findIndex() stops when it finds the index of the first qualified value

{
  console.log([1,2,3,4,5,6].find(function(item){return item>3}));//4
  console.log([1,2,3,4,5,6].findIndex(function(item){return item>3}));
}
{
  console.log('number',[1,2,NaN].includes(1));//true
  console.log('number',[1,2,NaN].includes(NaN));//true
}

The above is the detailed content of Detailed explanation of new feature examples of array extension. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php如何使用PHP的SNMP扩展?php如何使用PHP的SNMP扩展?Jun 02, 2023 am 10:22 AM

PHP的SNMP扩展是一种使PHP能够通过SNMP协议与网络设备进行通信的扩展程序。使用该扩展可以方便地获取和修改网络设备的配置信息,例如路由器、交换机等设备的CPU、内存、网络接口等信息,也可以进行诸如开关设备端口等控制操作。本文将介绍SNMP协议的基础知识、PHP的SNMP扩展的安装方法以及如何在PHP中使用SNMP扩展进行网络设备的监控和控制。一、SN

如何使用极光推送扩展,在PHP应用中实现批量消息推送功能如何使用极光推送扩展,在PHP应用中实现批量消息推送功能Jul 25, 2023 pm 08:07 PM

如何使用极光推送扩展,在PHP应用中实现批量消息推送功能在移动应用的开发中,消息推送是一项非常重要的功能。极光推送是一种常用的消息推送服务,提供了丰富的功能和接口。本文将介绍如何使用极光推送扩展在PHP应用中实现批量消息推送功能。第一步:注册极光推送账号并获取API密钥首先,我们需要在极光推送官网(https://www.jiguang.cn/push)注册

php如何使用PHP的ZipArchive扩展?php如何使用PHP的ZipArchive扩展?Jun 02, 2023 am 08:13 AM

PHP是一种流行的服务器端语言,可以用来开发Web应用程序和处理文件。PHP的ZipArchive扩展是一个强大的工具,可以在PHP中操作zip文件。在这篇文章中,我们将介绍如何使用PHP的ZipArchive扩展来创建、读取和修改zip文件。一、安装ZipArchive扩展在使用ZipArchive扩展之前,需要确保已经安装了这个扩展。安装方法如下:1.安

php如何使用PHP的Phar扩展?php如何使用PHP的Phar扩展?May 31, 2023 pm 11:31 PM

随着PHP的发展和应用场景的不断扩大,Phar扩展已经成为PHP编程中的重要一环。Phar是PHPArchive的缩写,它可以将多个PHP文件和资源打包成单个文件,方便进行分发和管理。本文将介绍如何使用PHP的Phar扩展来进行打包和管理。安装Phar扩展首先,我们需要检查PHP是否已经安装Phar扩展。在Linux下,通过终端输入以下命令:php-m

php如何使用PHP的POSIX扩展?php如何使用PHP的POSIX扩展?Jun 03, 2023 am 08:01 AM

PHP的POSIX扩展是一组允许PHP与POSIX兼容操作系统进行交互的函数和常量。POSIX(PortableOperatingSystemInterface)是一组操作系统接口标准,旨在允许软件开发人员编写可在各种UNIX或UNIX类操作系统上运行的应用程序。本文将介绍如何使用PHP的POSIX扩展,包括安装和使用。一、安装PHP的POSIX扩展在

Vue如何实现组件的复用和扩展?Vue如何实现组件的复用和扩展?Jun 27, 2023 am 10:22 AM

随着前端技术的不断发展,Vue已经成为了前端开发中的热门框架之一。在Vue中,组件是其中的核心概念之一,它可以将页面分解为更小,更易管理的部分,从而提高开发效率和代码复用性。本文将重点介绍Vue如何实现组件的复用和扩展。一、Vue组件复用mixinsmixins是Vue中的一种共享组件选项的方式。Mixins允许将多个组件的组件选项合并成一个对象,从而最大

PHP和WebDriver扩展:如何模拟用户点击和输入操作PHP和WebDriver扩展:如何模拟用户点击和输入操作Jul 07, 2023 pm 05:10 PM

PHP和WebDriver扩展:如何模拟用户点击和输入操作近年来,随着Web应用程序的快速发展,自动化测试变得越来越重要。在自动化测试中,模拟用户操作是一个关键的环节,它可以使我们更准确地测试和验证我们的应用程序。在PHP开发中,我们通常使用SeleniumWebDriver来实现自动化测试。SeleniumWebDriver是一种强大的工具,它可以模拟

教程:使用百度云推送(Baidu Push)扩展在PHP应用中实现消息推送功能教程:使用百度云推送(Baidu Push)扩展在PHP应用中实现消息推送功能Jul 26, 2023 am 09:25 AM

教程:使用百度云推送(BaiduPush)扩展在PHP应用中实现消息推送功能引言:随着移动应用的迅猛发展,消息推送功能在应用程序中变得越来越重要。为了实现即时通知和消息推送功能,百度提供了一种强大的云推送服务,即百度云推送(BaiduPush)。在本教程中,我们将学习如何使用百度云推送扩展(PHPSDK)在PHP应用中实现消息推送功能。我们将使用百度云

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools