Array method in the method of JavaScript language essence

Array

  • array.concat(item...)
            var a=[1,2,3];
            var b=[4,5,6];
            var c=a.concat(b);
            console.log(a); //(3) [1, 2, 3]
            console.log(b); //(3) [4, 5, 6]
            console.log(c); //(6) [1, 2, 3, 4, 5, 6]
  • array.join(separator)
    The join method first constructs each element in array into a string, and then joins them together with a separator separator separator.
            var a=['a','b','c'];
            a.push('d');
            var c=a.join('');
            console.log(a); //(4) ["a", "b", "c", "d"]
            console.log(c); //'abcd'
  • array.pop()
            var a=['a','b','c'];
            var c=a.pop();
            console.log(a); //(2) ["a", "b"]
            console.log(c); // c

The pop method can be implemented like this:

            Array.method('pop',function(){
                return this.splice(this.length-1,1)[0];
            });
  • array.push(item...)
            var a=['a','b','c'];
            var b=['x','y','z'];
            var c=a.push(b,true);
            console.log(a); //(5) ["a", "b", "c", ['x','y','z'] , true]
            console.log(b); //(3) ["x", "y", "z"]
            console.log(c);//5
  • array.reverse()
            var a=['a','b','c'];
            var b=a.reverse();
            console.log(a); //(3) ["c", "b", "a"]
            console.log(b); //(3) ["c", "b", "a"]
  • array.shift()
            var a=['a','b','c'];
            var c=a.shift();
            console.log(a); //(2) ["b", "c"]
            console.log(c); //a
  • array.slice(start,end)
    If either of the two parameters is a negative number, array.length adds them to make them nonnegative.
            var a=['a','b','c'];
            var b=a.slice(0,1);
            console.log(a); //(3) ["a", "b", "c"]
            console.log(b); //["a"]
            var c=a.slice(1); 
            console.log(a); //(3) ["a", "b", "c"]
            console.log(c); //(2) ["b", "c"]
            var n=[4,5,8,35,43];
            n.sort();
            console.log(n);//(5) [35, 4, 43, 5, 8]
  • array.splice(start,deleteCount,item...)
            var a=['a','b','c'];
            var r=a.splice(1,1,'ache','bug');
            console.log(a); //(4) ["a", "ache", "bug", "c"]
            console.log(r); //["b"]
  • array.unshift(item..)
            var a=['a','b','c'];
            var r=a.unshift('?','$');
            console.log(a); //(5) ["?", "$", "a", "b", "c"]
            console.log(r); //5

Added by dellvostro on Fri, 01 May 2020 06:21:33 +0300