let ass='[{"name":"ss","age":"3"},{"name":"aa","age":"1"},{"name":"qq","age":"4"},{"name":"zz","age":"3"},{"name":"xx","age":"5"},{"name":"cc","age":"7"}]'
將JSON字符串轉換成JSON對象(JSON反序列化) let aa=JSON.parse(ass); console.log(aa[2].age);//4
將JSON對象轉換成JSON字符串(JSON序列化) let arr=[ {"name":"ss","age":"3"}, {"name":"aa","age":"1"}, {"name":"qq","age":"4"}, {"name":"zz","age":"3"}, {"name":"xx","age":"5"}, {"name":"cc","age":"7"} ] let bb=JSON.stringify(arr); console.log(bb);//[{"name":"ss","age":"3"},{"name":"aa","age":"1"},{"name":"qq","age":"4"},{"name":"zz","age":"3"},{"name":"xx","age":"5"},{"name":"cc","age":"7"}]
會刪除一個字符串兩端的空白字符。並不影響原字符串自己,它返回的是一個新的字符串。 let str22=' fff '; console.log("+"+str22+"+");//+ fff + console.log("+"+str22.trim()+"+")//+fff+
//1970.7.1到如今,1秒=1000毫秒這個機率算 console.log(Date.now());//1500799156134 //toJSON()格林時間(德國) 與咱們的時間正好相反 console.log(new Date().toJSON())//2017-07-23T08:42:13.050Z console.log(new Date().toJSON().slice(0,10))//2017-07-23
小數後取位數,數字表明取到第幾位,四捨五入返回小數 let a=new Number(1.234); console.log(a.toFixed(1));//1.2 let v=new Number(4.645372); console.log(v.toFixed(2));//4.56 console.log(Math.round(13.89));//14 四捨五入返回整數
以指定的精度返回該數值對象的字符串表示。 precision 是 1 ~ 21 之間 let d=100000000000000000000; console.log(d.toPrecision(7));1.000000e+20 console.log(d.toPrecision(3));1.00e+20
let arr=[1,2,3,4,5]; arr.forEach(function(value, index){ console.log(value);//1,2,3,4,5 console.log(index);//0,1,2,3,4 });
let arr1=arr.map(function(value){ //return value+"abc"; return value*2+1; }); console.log(arr1);// ["1abc", "2abc", "3abc", "4abc", "5abc"] console.log(arr1);//[3, 5, 7, 9, 11]
let arr2=arr.filter(function(value){ return value>2; }); console.log(arr2);//[3, 4, 5]
let arr3=arr.some(function(value){ return value>2; }); console.log(arr3);//true
let arr4=arr.every(function(value){ return value>3; }); console.log(arr4);//false
返回整數索引值,若是沒有匹配(嚴格匹配),返回-1. 匹配到返回 1。es6
var aa = [2, 5, 7, 3, 5]; console.log(aa.indexOf(5, "x")); // 1 ("x"被忽略) console.log(aa.indexOf(5, "3")); // 4 (從3號位開始搜索) console.log(aa.indexOf(4)); // -1 (未找到) console.log(aa.indexOf("5")); // -1 (未找到,由於5 !== "5")
console.log(aa.lastIndexOf(5)); // 4 //console.log(aa.lastIndexOf(5, 3)); // 1 (從後往前,索引值小於3的開始搜索) //console.log(aa.lastIndexOf(4)); // -1 (未找到)
let ar3=[1,2,3,4,5]; let gh=ar3.reduce(function(last,now){ return last*now; },1) console.log(gh);//120
至關於reduce,只是reduceRight從右邊開始計算數組
let ar33=[1,2,3,4,5]; let gh22=ar33.reduceRight(function(last,now){ return last*now; },1) console.log(gh22);//120
判斷一個變量是否爲數組,是返回true,不是返回falseapp
let ss=22; console.log(Array.isArray(ss));//false console.log(Array.isArray(ar33));//true ps:不兼容IE8.
let obj={ x:1, y:2 } let obj2=Object.create(obj,{z:{value:3}}); console.log(obj2);//Object {z: 3} console.log(obj2.x);//1 //es6 let obj3={ __proto__:obj, z:3 }; console.log(obj3);//Object {z: 3}
let objj={ x:1, y:2 }; objj.k=4; Object.defineProperty(objj,"j",{ value:5, writable:false,//可寫 enumerable:false,//可便利 configurable: true//可配置 }); objj.j=50; objj.k=50; console.log(objj);//Object {x: 1, y: 2, k: 50, j: 5}
let objj={ x:1, y:2 }; Object.defineProperties(objj,{ "m":{ value:"mm", }, "u":{ value:"ll", } }) console.log(objj);//Object {x: 1, y: 2, m: "mm", u: "ll"}
let sdf=Object.getOwnPropertyDescriptor(objj,'x'); console.log(sdf);//Object {value: 1, writable: true, enumerable: true, configurable: true}
console.log(Object.keys(obj));// ["x", "y"]
console.log(Object.getOwnPropertyNames(obj));
/*let aa={ x:1, y:2 }; Object.preventExtensions(aa); aa.fn=function(){ } console.log(aa);//Object {x: 1, y: 2}*/
let aa={ x:1, y:2 }; Object.seal(aa); aa.fn=function(){ } console.log(aa);//Object {x: 1, y: 4}
let aa={ x:1, y:2 }; Object.freeze(aa); aa.y=4; console.log(aa);//Object {x: 1, y: 2}
console.log(Object.isExtensible(aa));//false
console.log(Object.isSealed(aa));//true
console.log(Object.isFrozen(aa));//true
ps 以上方法均不兼容ie8函數