# js中Array方法

121 篇文章 3 订阅
这段代码展示了JavaScript中数组的一些基本操作,如concat()合并数组,pop()移除最后一个元素,push()添加元素,reverse()反转数组,shift()移除第一个元素,slice()截取子数组,splice()插入/删除元素,以及unshift()在开头添加元素。
摘要由CSDN通过智能技术生成
var parents = ["Jani", "Tove"];
var brothers = ["Stale", "Kai Jim", "Borge"];
var children = ["Cecilie", "Lone"];
var family = parents.concat(brothers, children);
console.log("p ",parents)
console.log("brothers ",brothers)
console.log("children ",children)
console.log("family ",family)
console.log("pop = ",family.pop())
console.log("f = ",family)
let num = family.push('yyyyy','zzz','aaa');
console.log('ff = ',family)
console.log('num = ',num)
let ft = family.reverse();
ft.pop();
console.log('ft = ',ft);
console.log("fan  = ",family)
family.shift()
console.log("shift = ",family)
let sl = family.slice(2,4);
console.log("sl = ",sl);
console.log('fa = ',family);

let sp = family.splice(2,3,'j1','j2')
console.log('splice  = ',family);
console.log("sp = ",sp);

let uncount = family.unshift('a1','a2');
console.log('uncount = ',uncount)
console.log('f = ',family);

结果:

p  [ 'Jani', 'Tove' ]
brothers  [ 'Stale', 'Kai Jim', 'Borge' ]
children  [ 'Cecilie', 'Lone' ]
family  [
  'Jani',  'Tove',
  'Stale', 'Kai Jim',
  'Borge', 'Cecilie',
  'Lone'
]
pop =  Lone
f =  [ 'Jani', 'Tove', 'Stale', 'Kai Jim', 'Borge', 'Cecilie' ]
ff =  [
  'Jani',  'Tove',
  'Stale', 'Kai Jim',
  'Borge', 'Cecilie',
  'yyyyy', 'zzz',
  'aaa'
]
num =  9
ft =  [
  'aaa',   'zzz',
  'yyyyy', 'Cecilie',
  'Borge', 'Kai Jim',
  'Stale', 'Tove'
]
fan  =  [
  'aaa',   'zzz',
  'yyyyy', 'Cecilie',
  'Borge', 'Kai Jim',
  'Stale', 'Tove'
]
shift =  [
  'zzz',     'yyyyy',
  'Cecilie', 'Borge',
  'Kai Jim', 'Stale',
  'Tove'
]
sl =  [ 'Cecilie', 'Borge' ]
fa =  [
  'zzz',     'yyyyy',
sp =  [ 'Cecilie', 'Borge', 'Kai Jim' ]
uncount =  8
f =  [
  'a1',    'a2',
  'zzz',   'yyyyy',
  'j1',    'j2',
  'Stale', 'Tove'
]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值