push与concat
push
push()方法将一个或多个元素添加到数组的末尾,并且返回新的数组长度。
语法:
arr.push(element1, ..., elementN)
concat
concat() 方法用于连接两个或多个数组。该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本。
语法:
var new_array = old_array.concat(value1[, value2[, ...[, valueN]]])
因此,push与concat的对原数组的操作是不同的,push是在原数组基础上操作的,而concat的原数组不会改变。
//push
var sports = ['soccer', 'baseball'];
var total = sports.push('football', 'swimming'); console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
//concat
var sports = ['soccer', 'baseball'];
var total = sports.concat('football', 'swimming'); console.log(sports); // ['soccer', 'baseball']
console.log(total); // ['soccer', 'baseball','football', 'swimming']
添加长度大于1的数组,push会将数组作为整体添加到数组末尾,而concat则不会
//push
var sports = ['soccer', 'baseball'];
var total = sports.push(['football', 'swimming']); console.log(sports); //["soccer", "baseball", Array[2]]0: "soccer"1: "baseball"2: Array[2]length: 3__proto__: Array[0] console.log(total); // 3
//concat
var sports = ['soccer', 'baseball'];
var total = sports.concat(['football', 'swimming']); console.log(sports); // ["soccer", "baseball"] console.log(total); // ["soccer", "baseball", "football", "swimming"]
那么在上一步的操作中如何使用push方法,使sports变成["soccer", "baseball", "football", "swimming"] ,而不是["soccer", "baseball", Array[2]]?[1]
首先push和concat均可使用call()和Apply(), 其中
apply()方法能劫持另外一个对象的方法,继承另外一个对象的属性.
Function.apply(obj,args)方法能接收两个参数
obj:这个对象将代替Function类里this对象
args:这个是数组,它将作为参数传给Function(args-->arguments)
call()和apply的意思一样,只不过是参数列表不一样.
Function.call(obj,[param1[,param2[,…[,paramN]]]])
obj:这个对象将代替Function类里this对象
params:这个是一个参数列表
从Apply()的定义上,你大概已经知道解决方法了,利用Apply,将数组作为参数传给push,这样就能得到你想要的结果了。
var sports = ['soccer', 'baseball'];
var total = Array.prototype.push.apply(sports,['football', 'swimming']);
/*or
var total = sports.push.apply(sports,['football', 'swimming']);
*/ console.log(sports); // ["soccer", "baseball", "football", "swimming"]
console.log(total); // 4
相关链接:
[1] stackoverflow How to extend an existing JavaScript array with another array? [duplicate]
push与concat的更多相关文章
- javascript push 和 concat 的区别
array.push(item1,item2,item3...) array.concat(item1,item2,item3...) 1. push和concat的元素都既可以是普通元素(任意类型) ...
- 微信小程序之数组操作:push与concat的区别
微信小程序中需要用到数组的操作,push和concat二者功能很相像,但有两点区别. 先看如下例子: var arr = []; arr.push(); arr.push(); arr.push([, ...
- JavaScript的push(),pop(),concat()方法
push 方法 将新元素添加到一个数组中,并返回数组的新长度值. arrayObj.push([item1 [item2 [. . . [itemN ]]]]) 参数 arrayObj 必选项.一个 ...
- JavaScript中push ,pop ,concat ,join方法
push 方法 将新元素添加到一个数组中,并返回数组的新长度值. arrayObj.push([item1 [item2[. . . [itemN ]]]]) 说明 push 方法将以新元素出现的顺序 ...
- js push ,pop ,concat ,join方法
push 方法 将新元素添加到一个数组中,并返回数组的新长度值. arrayObj.push([item1 [item2[. . . [itemN ]]]]) 说明 push 方法将以新元素出现的顺序 ...
- JavaScript引用类型之Array数组的concat()和push()方法的区别
在javascript中,我们一般都只用push向数组的尾部插入新元素的,但是其实在javascript中还有另外一个方法和push一样,也是向数组尾部插入新元素的,但是他们之间却存在着一定的区别,当 ...
- 微信小程序中concat 和push的区别
push和concat二者功能很相像,但有两点区别. 先看如下例子: var arr = []; arr.push(1); arr.push(2); arr.push([3, 4]) arr.push ...
- [Redux] Avoiding Array Mutations with concat(), slice(), and ...spread
For Redux, you cannot use mutable methods like push, splice. Need to use immutable methods such as c ...
- JavaScript—从数组的indexOf方法深入——Object的Property机制。
在js中,可以说万物皆对象(object),一个数组也是一个对象(array). 很多对象都有很多很方便的方法 比如数组的push,concat,slice等等,但是如果一些对象,它没有实现这些方法, ...
随机推荐
- UITableView 应用及其总结
Plain: Grouped: Cell的结构图: UITableViewCellStyleDefault:预设使用这种,若左侧ImageView没图的话,只有一行字(textLable.text). ...
- 【转】PLSQL developer 连接不上64位Oracle 的解决方法
PLSQL developer 连接不上64位Oracle 的解决方法 快乐无极 , 2012/06/13 10:10 , 开发文档 , 评论(6) , 阅读(140430) , Via 本站原创 大 ...
- 【Integer To Roman】cpp
题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...
- sqlserver 查看锁表,解锁
查看被锁表: 代码如下 复制代码 select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName ...
- EF 更新条目时出错。有关详细信息,请参见内部异常。
现象:使用EF新增记录时,一直报上述异常,网上说是值为空.主键外键未设等原因导致,但是改正这些情况下问题依然 解决过程:异常中有一句(请参见内部异常),一直都没有当回事,后来实在没办法就静下心来看了看 ...
- 【python】正则表达式
参考资料:http://deerchao.net/tutorials/regex/regex.htm 1.正则表达式基础 2.python 正则表达式 1.正则表达式基础 元字符: 其他语法: (1) ...
- bzoj 2002 LCT
LCT最基础的题,就用到了一个ACCESS操作 首先我们将这个绵羊弹飞的情况看成一颗树,那么假设X点被弹飞到 Y点,那么Y为X的父亲节点,弹飞的话父亲节点为n+1(虚设) 那么每个询问就是询问X点到根 ...
- IDA*
模拟退火 基本思路(Main Thoughts): IDA*是一种优秀的搜索法,在一般的实际问题中,它比普通的搜索更快. 通过迭代加深和估价函数剪枝来搜索. 通常处理没有层数上界或上界很多大的搜索. ...
- PKUSC滚粗记
本来想考得这么烂还是别写了,后来想想毕竟是我高中难得的一次经历,靠脑子记的话我这脑残患者将来肯定会忘了啊……QwQ 好像跟我一样用这个题目的神犇都签了一本QwQ Day 0 来的路上我校其他三位OIe ...
- Coding4Fun.Phone.Controls的使用
Coding4Fun.Phone.Controls的使用: windows phone的应用一直有一个特色,那就是方块(磁贴).之前的应用中,我一直都XXXX 来实现,原来其实一直有一个更加好的方法, ...