1逢捺,刪除數(shù)組尾部元素
改變數(shù)組的length值就可以刪除
const arr = [0,1,2,3,4,5];
arr.length = 3;
console.log(arr); //=>[0,1,2]
arr.length = 0;
console.log(arr); //=>[]
console.log(arr[2]);//=>undefined
2丧没,數(shù)組解構(gòu)
const csvFileLine = '1997,John Doe,US,john@doe.com,New York';
console.log(typeof csvFileLine); //=> string
//split方法會將字符串變成object人弓,但是不會影響原值
console.log(csvFileLine.split(','));//=>["1997", "John Doe", "US", "john@doe.com", "New York"]
const { 2: country, 4: state } = csvFileLine.split(',');
console.log(country);//=>'US'
console.log(state);//=>'New York'
這里介紹一下typeof 各種數(shù)據(jù)類型
console.log(typeof 2); //=>number
console.log(typeof 'abc');//=>string
console.log(typeof true);//=>boolean
console.log(typeof null);//=>object
console.log(typeof undefined);//=>undefined
console.log(typeof {});//=>object
console.log(typeof []);//=>object
console.log(typeof (function () {}));//=>function
那么怎么區(qū)別{}沼死,[],null的類型呢崔赌?
1意蛀,使用jQuery
2,使用原生原型擴(kuò)展函數(shù)
jquery方法
直接使用jQuery.type健芭,另外還可以判斷日期date和正則regexp
使用原生擴(kuò)展函數(shù)
let getType=Object.prototype.toString
getType.call('aaaa') //=> [object String]
getType.call(2222) //=> [object Number]
getType.call(true)//=> [object Boolean]
getType.call(undefined)//=> [object Undefined]
getType.call(null)//=> [object Null]
getType.call({})//=> [object Object]
getType.call([])//=> [object Array]
getType.call(function(){})//=> [object Function]
另外在判斷空對象和空數(shù)組的時候不能直接判斷县钥,因?yàn)榭諗?shù)組和空對象都是true
如何判斷對象為空
可以使用ES6
let data = {};
let arr = Object.keys(data);
console.log(arr.length==0);//=>true
可以使用jQuery
let data = {};
let arr = jQuery.isEmptyObject(data);
console.log(arr);//=>true
for in 判斷
var obj = {};
var b = function() {
for(var key in obj) {
return false;
}
return true;
}
alert(b());//true
將json對象轉(zhuǎn)化為json字符串,再判斷該字符串是否為"{}"
var data = {};
var b = (JSON.stringify(data) == "{}");
alert(b);//true
如何判斷數(shù)組為空
var a = []
if(Object.prototype.toString.call(a) === '[object Array]' && a.length === 0){
console.log(true)
}