js对数组的操作 数据间转换

//数组元素的添加
arr.push([item1],[item2]...[itemn]);//将一个或者多个元素添加到数组结尾,返回数组新长度
arr.unshift([item1],[item2]...[itemn]);//将一个或者多个元素添加到数组开始,数组自动后移
arr.splice(insertPos,0,[item1],[item2]...[itemn]);//将一个或者多个元素从insertPos位置开始插入,插入位置的元素自动后移
//数组元素的删除
arr.pop();//移除最后一个元素并且返回该元素值
arr.shift();//移除最前一个元素并且返回该元素值
arr.splice(deletePos,deleteCount);//删除从deletePos位置开始的数量deleteCount元素,数组形式返回所移除的元素
//数组的截取和合并
arr.slice(start,[end]);//返回从start到end-1的元素
arr.concat([item1],[item2]...[itemn]);//连接多个数组元素
//数组的拷贝
arr.slice(0);
arr.concat();
//数组的排序
arr.reverse();//数组反转
arr.sort();//数组排序
//数组元素的字符串化
arr.join(separator);返回以分隔符separator分割的字符串
//数组的prototype属性
function array_max(){
    var i,max = this[0];
    for(i = 1;i<this.lenght;i++){
        if(max<this[i])
        max= this[i];
    }
    return max;
}
Array.prototype.max = array_max;
var x = new Array(1,2,3,4,5);
var y = x.max();
//数组的constructor属性
x = new String("Hi");
if(x.constructor == string)//判断结果为真
//js splice()的用法
splice(0,2)会删除数组的前两项
splice(2,0,"red","green")会从位置2开始插入red、green
splice(2,2,"red","green")删除当前位置为2的项,再从位置2开始插入red、green

 

 

var str = '{"name":"huangxiaojian","age":"23"}'

var t=JSON.parse(str);
console.log(str.name);
console.log(t.name);


var a = {a:1,b:2};
var b=JSON.stringify(a);
console.log(a.a);
console.log(b.a);

 

parse用于从一个字符串中解析出json对象,如

var str = '{"name":"huangxiaojian","age":"23"}'

结果:

JSON.parse(str)



Object

age: "23"
name: "huangxiaojian"
__proto__: Object



注意:单引号写在{}外,每个属性名都必须用双引号,否则会抛出异常。



stringify()用于从一个对象解析出字符串,如


var
 a = {a:1,b:2}

结果:

JSON.stringify(a)


"{"a":1,"b":2}"

  

 

		var arr1=[
			{a:'188',b:'1'},
			{a:'100',b:'2'},
			{a:'108',b:'3'},
			{a:'11',b:'4'},
			{a:'18',b:'5'},
			{a:'280',b:'6'}
		];
		
		//遍历输出
		for(var i=0;i<arr1.length;i++){
			console.log(arr1[i].a);
		};

		//在数组中插入
		var arr2=[1,2,17,4,5,6,7,8,9,1];
		var len=arr2.length/2;
		var attr=Number.isInteger(len);
		if(attr){
			arr2.splice(len,0,100);
			console.log(arr2);
		}else{
			var k=parseInt(len);
			arr2.splice(len,0,100);
			arr2.splice(len+2,0,101);
			console.log(arr2);
		}

		//去重
		var Narr2=[];
		for(var i=0;i<arr2.length;i++){
			var attr=true;
			for(var j=0;j<Narr2.length;j++){
				if(arr2[i]==Narr2[j]){
					attr=false;
				}
			};
			if(attr){
				Narr2.push(arr2[i]);
			}
		}
		console.log(Narr2);


		//数组转字符串
		var arrSimple=[1,8,7,17,6,2,5];
        console.log(arrSimple.join('-'));

        //字符串转数组
        var arr4="1-2-3-4-7-8-6-78";
        var st=arr4.split('-');
        console.log(st);


        //排序sort
		var arrSimple=[1,8,7,17,6,2,5];
		arrSimple.sort();
        console.log(arrSimple.join());

        var arrSimple2=new Array(1,8,17,6);
        arrSimple2.sort(function(a,b){
            return b-a});
        console.log(arrSimple2.join());


        var objectList = new Array();
        function Persion(name,age){
            this.name=name;
            this.age=age;
            }
        objectList.push(new Persion('jack',20));
        objectList.push(new Persion('tony',25));
        objectList.push(new Persion('stone',26));
        objectList.push(new Persion('mandy',23));
        //按年龄从小到大排序
        objectList.sort(function(a,b){
            return a.age-b.age});
        for(var i=0;i<objectList.length;i++){
        	console.log(objectList[i].age);
        }

  

posted @ 2017-03-27 16:36  fm060  阅读(487)  评论(0)    收藏  举报