자바 스 크 립 트 의 배열 연습 노트 를 처음 배 웠 습 니 다.

83005 단어 JavaScript
"    ==============".description();
Array.prototype.name = "Array";
var arr3 = [1,2,3,4,5,6,7,8,9,0];
for(var i in arr3)
{
	arr3[i].description();
}
"--------".description();
for(var i in arr3)
{
	if (! arr3.hasOwnProperty(i)) continue;//       
	arr3[i].description();
}

"--------".description();

arr3.forEach(function(){
	arguments.description();
})


"    ==============".description();
var table = new Array(10);
for (var i = 0; i < table.length; i++) {
	table[i] = new Array(10);
}
for(var row = 0; row < table.length; row++)
{
	for (var col = 0; col < table[row].length; col++) {
		table[row][col] = row * col;
	}

}	
table.description();
table[2].description();
table[2][8].description();

"    ==============".description();
/**
// join()            
**/
"----------join".description();
table.join("").description();
table.join("******").description();
table.description();

/**
// reverse()            ,       。
**/
"----------reverse".description();
var mArr = [1,2,3,4,5];
mArr.reverse().description();
mArr.description();

/**
// sort()                   。        ,  ASCII   
**/
"----------sort".description();
var mArr = new Array("aadf","Basd","Adfd","Casdf","cads","123");
mArr.sort().description();
mArr.description();

var mArr = new Array(8,3,4,9,123,444,66,88);
mArr.sort().description();

mArr.sort(function(a,b){
	// arguments.length.description();
	return a > b;
}).description();

mArr.sort(function(a,b){
	// arguments.length.description();
	return a < b;
}).description();
// a   b        0 , a b       0


/**
//concat             ,        concat()      concat()     
**/
"----------concat".description();
var arr5 = [1,2,3,4,5];
arr5.concat(4,5,[66,88],[77,[123,456]]).description();

/**
// slice                   。                 [star,end)
**/
"----------slice".description();
arr5.slice(1,3).description();
arr5.slice(2).description();
arr5.slice(1,-1).description();
arr5.slice(-4,-2).description();
arr5.description();

/**
// splice                  。         
//      :             
//      :               。(      ,                  )
//    :          。
**/
 "----------splice".description();
var arr6 = [1,2,3,4,5,6,7,8,9,0];
arr6.splice(4).description()  //    [4,end]
arr6.description();
"--".description();
var arr6 = [1,2,3,4,5,6,7,8,9,0];
arr6.splice(2,4).description();
arr6.description();
"--".description();
var arr6 = [1,2,3,4,5,6,7,8,9,0];
arr6.splice(1,1).description();
arr6.description();
"--".description();
var arr6 = [1,2,3,4,5,6,7,8,9,0];
arr6.splice(2,0,['a','b']).description();//     2           
arr6.description();
"--".description();
var arr6 = [1,2,3,4,5,6,7,8,9,0];
arr6.splice(2,2,'a','b').description();//     2、3,     2         
arr6.description();

/**
// push()   pop()
//                 。push()            ,      
// pop()           ,    ,       
**/
 "---------- push()   pop()".description();
 var stack = [];
 stack.push(1,2,6).description();    // [Number: 3]
 stack.description();                // [ 1, 2 ,6]
 stack.pop().description();          // [Number: 6]
 stack.push(8,9);                    
 stack.push([11,22,33]);             
 stack.push();
 stack.description();                // [ 1, 2, 8, 9, [ 11, 22, 33 ] ]
 stack.length.description();         // [Number: 5]
 stack.pop().description();          // [ 11, 22, 33 ]
 stack.description();                // [ 1, 2, 8, 9 ]


/**
// unshift()   shift()
// unshift()            ,                          ,        。
// shift()               ,      ,    
//   :unshift()         ,
**/
 "---------- unshift()   shift()".description();
 var mArray = [];
 mArray.unshift(1).description();                  // [Number: 1]
 mArray.unshift(22);
 mArray.description();                             // [ 22, 1 ]
 mArray.length.description();                      // [Number: 2]

 mArray.shift().description();                     // [Number: 22]
 mArray.description();                             // [ 1 ]
 mArray.length.description();                      // [Number: 1]
 mArray.unshift(88,['a','b']).description();       // [Number: 3]
 mArray.description();                             // [ 88, [ 'a', 'b' ], 1 ]



/**
// toString()                               。
// toLocaleString() toString()        。
**/
 "---------- toString()   toLocaleString()".description();             
[1,2,3,4,5].toString().description();                          //[String: '1,2,3,4,5']
["a","b","c"].toString().description();                        //[String: 'a,b,c']
[1,2,3,'a','b','c'].toString().description();                  //[String: '1,2,3,a,b,c']
[1,2,[88,99],['a','c'],[666,'520']].toString().description();  //[String: '1,2,88,99,a,c,666,520']


/**
 *  map()                     ,       ,          。
 */
"---------- map()".description();  
var array1 = [1,2,3,4];
var array2 = array1.map(function(value,index,array){
	return value * value;
}) 
array2.description();  //[ 1, 4, 9, 16 ]

var array3 = array1.map(function(){
}) 
array3.description();  //[ undefined, undefined, undefined, undefined ]


/**
 * filter()                     。
 *              ,     true/false.(   true          )
 *   : filer()             ,           。
 */
"---------- filter()".description(); 
var array2 = [9,8,7,6,5,4,3,2,1,0];
var subArr = array2.filter(function(value){
	return !(value % 2);
})
subArr.description();           // [ 8, 6, 4, 2, 0 ]
array2.description();           // [ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ]
//              
var sparse = new Array(3);
sparse[0] = sparse[2] = "lalal";
sparse.description();           // [ 'lalal', <1 empty item>, 'lalal' ]
var dense = sparse.filter(function(){return true;});
dense.description();            // [ 'lalal', 'lalal' ]


/**
 * every()   some()          :                  ,  ture false
 * every()           true         true
 * some()              true     ture
 */
"---------- every()   some()".description()
var array3 = [1,2,3,4,5,6,7,8,9];
array3.every(function(value){ return value < 10}).description(); // [Boolean: true]
array3.every(function(value){ return value < 5}).description();  // [Boolean: false]

array3.some(function(value){ return value > 10}).description();  // [Boolean: false]
array3.some(function(value){ return value > 5}).description();   // [Boolean: true]


/**
 * reduce()   reduceRight()                   ,     。
 *              ,      "  "   "  "。
 *
 * reduce     ,             ,         (     ,          )。
 *        ,       ,       value 、index array。
 *                  reduce()      ,             
 *
 * reduceRight()   reduce    ,             。
 */
"---------- reduce()   reduceRight()".description()
var array4 = [1,2,3,4,5,6,7,8,9];
//   
var sum = array4.reduce(function(initNumb,value,index,array){
	return initNumb + value;
},0);
sum.description();      // [Number: 45]
//   
var product = array4.reduce(function(initNumb,value,index,array){
	return initNumb * value;
},1);
product.description();  // [Number: 362880]
//      
var max = array4.reduce(function(initNumb,value,index,array){
	return (initNumb > value) ? initNumb : value;
},0);
max.description();      // [Number: 9]

//        9 (          9)
var max = array4.reduceRight(function(){
	let initNumb = arguments[0];
	let value =arguments[1];
	return (initNumb > value) ? initNumb : value;
});
max.description();      // [Number: 9]

// union()  ,        "  ",        ,          
var objects = [{x:1},{y:2},{z:3}];
var merged = objects.reduce(union);
merged.description();   //{ x: 1, description: [Function], y: 2, z: 3 } //  description  

/**
 *  p          o ,   o
 *   o p      ,   o    
 *         getter setter      
 */
function extend(o,p)
{
	for(prop in p)
	{
		o[prop] = p[prop];
	}
	return o;
}
/**
 *        ,        o p   
 *   o p      ,  p     
 */
function union(o,p){return extend(extend({},o),p);}

var objects = [{x:1,a:1},{y:2,a:2},{z:3,a:3}];
objects.reduce(union).description();       //{ x: 1, a: 3, description: [Function], y: 2, z: 3 }  //  description  
objects.reduceRight(union).description();  //{ z: 3, a: 1, description: [Function], y: 2, x: 1 }  //  description  


/**
 * indexOf()  lastIndexOf()              ,          (      -1)
 * indexOf()       
 * lastIndexOf()       。(                )
 *
 *             ,         :         ,         
 */
"---------- indexOf()   lastIndexOf()".description()
var a = [0,1,2,0,3,4,6];
a.indexOf(2).description();          // [Number: 2]
a.lastIndexOf(4).description();      // [Number: 5]
a.indexOf(9).description();          // [Number: -1]
a.indexOf(0,2).description();        // [Number: 3]
/**
 *                  
 */
var b = [1,2,3,1,3,5,6,7,3,45,7,2,3,4,6,1,2,4,1];
function findall(array,x)
{
	var result = [],
	    len = array.length;
	    pos = 0;
	while(pos < len){
		pos = array.indexOf(x,pos);
		if (pos === -1) break;
		result.push(pos);
		pos++;
	}
	return result;
}
findall(b,1).description();   // [ 0, 3, 15, 18 ]



"    ==============".description();
//            
Array.isArray([]).description();   // [Boolean: true]
Array.isArray({}).description();   // [Boolean: false]

"     ==============".description();
var a = {};
var i = 0;
while(i < 10){
	a[i] = i * i;
	++i;
}
a.length = i;
//             
var total = 0;
for(var j = 0; j< a.length; j++) total += a[j];
total.description();

a.description();
//      Function.call     
Array.prototype.join.call(a,"+").description(); // [String: '0+1+4+9+16+25+36+49+64+81']
//         
Array.prototype.slice.call(a,0).description();  // [ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 ]
Array.prototype.map.call(a,function(value){
	return value * 10;
}).description();                               //[ 0, 10, 40, 90, 160, 250, 360, 490, 640, 810 ]

/**
 *                       ,    。  :Array.join()
 *                         
 */
Array.join = Array.join || function(array,sep){
	return Array.prototype.join.call(array,sep);
}


"        ==============".description();
var str = "test";
str.charAt(0).description();
str.charAt(2).description();
str[1].description();

좋은 웹페이지 즐겨찾기