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的更多相关文章

  1. javascript push 和 concat 的区别

    array.push(item1,item2,item3...) array.concat(item1,item2,item3...) 1. push和concat的元素都既可以是普通元素(任意类型) ...

  2. 微信小程序之数组操作:push与concat的区别

    微信小程序中需要用到数组的操作,push和concat二者功能很相像,但有两点区别. 先看如下例子: var arr = []; arr.push(); arr.push(); arr.push([, ...

  3. JavaScript的push(),pop(),concat()方法

    push 方法 将新元素添加到一个数组中,并返回数组的新长度值. arrayObj.push([item1 [item2 [. . . [itemN ]]]]) 参数 arrayObj 必选项.一个  ...

  4. JavaScript中push ,pop ,concat ,join方法

    push 方法 将新元素添加到一个数组中,并返回数组的新长度值. arrayObj.push([item1 [item2[. . . [itemN ]]]]) 说明 push 方法将以新元素出现的顺序 ...

  5. js push ,pop ,concat ,join方法

    push 方法 将新元素添加到一个数组中,并返回数组的新长度值. arrayObj.push([item1 [item2[. . . [itemN ]]]]) 说明 push 方法将以新元素出现的顺序 ...

  6. JavaScript引用类型之Array数组的concat()和push()方法的区别

    在javascript中,我们一般都只用push向数组的尾部插入新元素的,但是其实在javascript中还有另外一个方法和push一样,也是向数组尾部插入新元素的,但是他们之间却存在着一定的区别,当 ...

  7. 微信小程序中concat 和push的区别

    push和concat二者功能很相像,但有两点区别. 先看如下例子: var arr = []; arr.push(1); arr.push(2); arr.push([3, 4]) arr.push ...

  8. [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 ...

  9. JavaScript—从数组的indexOf方法深入——Object的Property机制。

    在js中,可以说万物皆对象(object),一个数组也是一个对象(array). 很多对象都有很多很方便的方法 比如数组的push,concat,slice等等,但是如果一些对象,它没有实现这些方法, ...

随机推荐

  1. JAVA素数分解

    package test; import java.util.*; public class test1 { public static void main(String[] args){ long ...

  2. quartz2D简单使用

    quartz2D绘图 1:上下文:context,这个翻译不好理解,其实翻译环境更好一点,就是给了你一个画板,你看不到而已 在: CGContextRef ctx = UIGraphicsGetCur ...

  3. 农行网银软件导致XP死机

    一台PC电脑安装了XP系统,突然会大约30分钟左右死机,键盘(CTRL+ALT+DEL)和鼠标都没反应,只能强行关机后重新开机.因为是用了几年的电脑,担心热稳定性问题,打开机箱后,发现CPU风扇很多灰 ...

  4. Facebook

    Facebook登录为iOS Facebook的SDK为iOS提供了各种登录的经验,你的应用程序可以使用它来 ​​验证一个人.这份文件包括了所有你需要知道,以落实Facebook登录在你的iOS应用程 ...

  5. html5离线存储

    为了提升Web应用的用户体验,我们在做网站或者webapp的时候,经常需要保存一些内容到本地.例如,用户登录token,或者一些不经常变动的数据. 随着HTML5的到来,出现了诸如AppCache.l ...

  6. 1-Highcharts 3D图之普通3D柱状图与带空值

    <!DOCTYPE> <html lang='en'> <head> <title>1-Highcharts 3D图之普通3D柱状图与带空值</t ...

  7. ios frame、bound和center定义及使用场景总结

    frame:指的是视图在父视图的坐标系统中的大小和位置. bound:指的是视图在视图本身的坐标系统中的大小(位置起点是原点). center:指的是视图在父视图坐标系统中的中心点. frame和bo ...

  8. 由浅入深了解Thrift之微服务化应用架构

    为什么选择微服务 一般情况下,业务应用我们都会采用模块化的分层式架构,所有的业务逻辑代码最终会在一个代码库中并统一部署,我们称这种应用架构为单体应用. 单体应用的问题是,全部开发人员会共享一个代码库, ...

  9. uva 11374

    Problem D: Airport Express In a small city called Iokh, a train service, Airport-Express, takes resi ...

  10. 在linux服务器上装svn版本管理,自动部署代码到项目

    在linux服务器上装svn版本管理,自动部署代码到项目 http://bbs.aliyun.com/read/9715.html?spm=5176.7114037.1996646101.1.W3zw ...