Last Updated: February 25, 2016
· richardgong1987
Psb  1

javascript Array slice, cancat methods

There various ways to work with the items already contained in an array.
The concat() and slice methods, for instance,use concat() method allow you to create a new array based on all of the item in the current array , consider this example:

 arr = ['a', 'b', 'c'],
 arr2 = arr.concat('aa', ['aaa', 'bbb', 'ccc']),
console.log(arr2); // ["a", "b", "c", "aa", "aaa", "bbb", "ccc"] 

concat() method begins by creating a copy of the array and then appending the method arguments
to the end and returning the newly constructed array.
when no arguments pass in ,they simply clones the current array and returns it, take an example:
arr = ['a', 'b', 'c'],
arr2 = arr.concat(),
console.log(arr2)//['a', 'b', 'c']

if more arrays are passed in concat() appends each item in current array to the end of the result:

consider this example:

arr = ['a', 'b', 'c'],
arr2 = arr.concat(true, 'aa', ['aaa', 'bbb', 'ccc', { a1: 'aaaa', b1: 'bbbb', c1: 'cccc'}, ['t1', 't2', 't3']]),

yup, we could pass arguments as a functions or javascript expression, it's very common when i write a javascript framework:

consider this example:

 var myarray, otherarray;
 arr = ['a', 'b', 'c'],
arr2 = arr.concat(true, (function () { return 'richardgong function' })(), myarray ? 'myarray have been defined' : 'myarray is undefined', otherarray && 'therarray have been defined', ('test', 'test2', 'test3')),
console.log(arr2);//["a", "b", "c", true, "richardgong function", "myarray is undefined", undefined, "test3"] 

slice(startingIndex, stopingIndex) method may accept one or two arguments, the starting and stopping positons of the item to return, (startingIndex indices start at 0):

arr = ['a', 'b', 'c', 'd', 'e'],
arr = arr.slice(3, 4), console.log(arr);//["d"] ;

arr = ['a', 'b', 'c', 'd', 'e'],
arr = arr.slice(0, (function () { return 1})()), console.log(arr);//["a"] 

var arg;
arr = ['a', 'b', 'c', 'd', 'e'],
arr = arr.slice(0, arg ? 0 : 1), console.log(arr);//["a"] 

var arg;
arr = ['a', 'b', 'c', 'd', 'e'],
arr = arr.slice(0, arg || (function(){ return 1})()), console.log(arr);//["a"] 

var arg;
arr = ['a', 'b', 'c', 'd', 'e'],
arr = arr.slice(0, arg && 1), console.log(arr);//["a", "b", "c", "d", "e"] 

arr = ['a', 'b', 'c', 'd', 'e'],
arr1 = arr.slice(0, arr.length >>> 0), console.log(arr1); //["a", "b", "c", "d", "e"] 

Perhaps the most powerfull array method is splice(), which can be used in a variety of ways.
there are three distinct ways of using this method . that's Delection, Insertion, Relacement.
we have been show the delection function of splice() method in above example.

take an example for it insertion:

 arr = ['a', 'b', 'c', 'd'],
arr.splice(0, 0, 'insert a1', 'insert a2'),
console.log(arr);//["insert a1", "insert a2", "a", "b", "c", "d"] 

 arr = ['a', 'b', 'c', 'd'],
 arr.splice(arr.length, 0, (function () { return 'richardong'})()),
 console.log(arr);//["a", "b", "c", "d", "richardong"] 

replacement function:

arr = ['a', 'b', 'c', 'd'],
arr.splice(arr.length - 1, 1, 'richardong'),
console.log(arr);//["a", "b", "c", "richardong"] 

in this example the item "d" was replaced as "richardgong"

Say Thanks