首页 > 其他分享 >js数组操作的shift unshift pop push用法

js数组操作的shift unshift pop push用法

时间:2023-08-22 11:14:53浏览次数:45  
标签:log shift unshift pop 数组 Expected output Array

Array.shift()

shift() 方法用在数组上, 移除数组的第一个元素并返回移除的元素. 该方法会改变原数组的长度.

const array1 = [1, 2, 3];
const firstElement = array1.shift();
console.log(array1); // Expected output: Array [2, 3]
console.log(firstElement); // Expected output: 1
如果数组为空,返回undefined
[].shift() // undefined


Array.unshift()

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.push()

The push() method of Array instances adds the specified elements to the end of an array and returns the new length of the array.

跟unshift用法一样,返回的也是新数组的长度。只不过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.pop()

The pop() method of Array instances removes the last element from an array and returns that element. This method changes the length of the array.

跟shift用法差不多,只不过shift从头部移除,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"]




参考:js数组操作的shift unshift pop push用法

标签:log,shift,unshift,pop,数组,Expected,output,Array
From: https://www.cnblogs.com/weimiu/p/17648007.html

相关文章

  • #pragma GCC diagnostic push 和 #pragma GCC diagnostic pop
    用途#pragmaGCCdiagnosticpush:用于记录当前的诊断状态#pragmaGCCdiagnosticpop:用于恢复诊断状态用法#pragmaGCCdiagnosticpush#pragmaGCCdiagnostic[kind]"option"/*code*/#pragmaGCCdiagnosticpush示例#pragmaGCCdiagnosticignored"option":忽略o......
  • POP3协议的历史及其工作原理
    POP3,全名为“PostOfficeProtocol-Version3”,即“邮局协议版本3”。是TCP/IP协议族中的一员,由RFC1939定义。POP3的具体历史可以追溯到1984年,由J.K.Reynolds带领的团队研发出了POP3协议的前身,POP1和POP2。到了1998年,POP3成为Internet标准,并持续发展和改进。虽然POP4曾被提出,但......
  • npm加参数--host启动报错 Could not auto-determine entry point from rollupOptions
    参考:https://blog.csdn.net/qq_41664096/article/details/118961381使用以下命令启动npm只能本机访问npmrundev如果需要网络访问则需要加参数--hostnpmrundev--host0.0.0.0会报错(!)Couldnotauto-determineentrypointfromrollupOptionsorhtmlfilesandt......
  • 无涯教程-Perl - unshift函数
    描述此函数按顺序将LIST中的元素放在ARRAY的开头。这与shift()相反。语法以下是此函数的简单语法-unshiftARRAY,LIST返回值此函数返回ARRAY中新元素的数量。例以下是显示其基本用法的示例代码-#!/usr/bin/perl-w@array=(1,2,3,4);print"Valueofarray......
  • 防止openlayers在draw绘制状态按shift功能失效
    newDraw({source:this.map.source,type:type,geometryFunction:geometryFunction,condition:()=>{returntrue;},freehandCondition:()=>{returnfalse;}});......
  • Hytec Inter HWL-2511-SS popen.cgi命令注入漏洞
    页面是以下这个屌样 poc/cgi-bin/popen.cgi?command=ping%20-c%204%201.1.1.1;cat%20/etc/shadow&v=0.1303033443137921 ......
  • Ant Design Pro项目Popover位置微调
    前情公司有经常需要做一些后台管理页面,我们选择了AntDesignPro,它是基于AntDesign和umi的封装的一整套企业级中后台前端/设计解决方案。产品效果图最新接到的一个后台管理界面需求,需要使用到Popover,但是在使用时发现那箭头位置不符合预期:理想:实际:解决方案从官网......
  • Python 优雅的使用 subprocess.Popen 获取实时输出,以及中止 subprocess
    #-*-coding:utf-8-*-importshleximportosimportsignalimporttimeimportthreadingfromsubprocessimportPopen,PIPEdefrun_command(command):process=Popen(shlex.split(command),stdout=PIPE)st=time.time()whileTrue:ou......
  • Android 5.0(Lollipop)中的SurfaceTexture,TextureView, SurfaceView和GLSurfaceView
    https://blog.csdn.net/jinzhuojun/article/details/44062175SurfaceView,GLSurfaceView,SurfaceTexture以及TextureView是Android当中名字比较绕,关系又比较密切的几个类。本文基于Android5.0(Lollipop)的代码理一下它们的基本原理,联系与区别。SurfaceView从Android1.0(API......
  • ACCESS 禁止用户使用SHIFT启动数据库
    这是官方链接,不要去各种百度了: 在数据库中强制实施或禁用启动选项-Office|MicrosoftLearn'1.启动访问。'2.创建新模块,然后添加以下两个函数:Functionap_DisableShift()'Thisfunctiondisabletheshiftatstartup.Thisactioncauses'theAutoexecmacroandS......