js判断数据类型(如数组)及数组操作函数

24 篇文章 1 订阅

在ES5的时候,js数据类型有6种:Number、String、Boolean、undefined、object、Null。

注:https://blog.csdn.net/u013592575/article/details/95087953

一、js判断类型

Object.prototype.toString可以看出一个值到底是什么类型。

Object.prototype.toString.call(2) // "[object Number]"
Object.prototype.toString.call('') // "[object String]"
Object.prototype.toString.call(true) // "[object Boolean]"
Object.prototype.toString.call(undefined) // "[object Undefined]"
Object.prototype.toString.call(null) // "[object Null]"
Object.prototype.toString.call(Math) // "[object Math]"
Object.prototype.toString.call({}) // "[object Object]"
Object.prototype.toString.call([]) // "[object Array]"

注:https://segmentfault.com/a/1190000013243913

二、数组函数

https://www.jianshu.com/p/66b04163948b

https://juejin.im/post/5b0903b26fb9a07a9d70c7e0

三、函数返回数组

function createFunction(){

		    var result = new Array();

		    for (var i = 0; i < 10; i++) {

		        result.push(i)

		    }

		    return result;

		}
console.log(createFunction());

http://www.imooc.com/wenda/detail/464877

https://wangdoc.com/javascript/types/array.html#forin-%E5%BE%AA%E7%8E%AF%E5%92%8C%E6%95%B0%E7%BB%84%E7%9A%84%E9%81%8D%E5%8E%86

四、数组元素的字符串化

join()方法是一个非常实用的方法,它把当前Array的每个元素都用指定的字符串连接起来,然后返回连接后的字符串

arrayObj.join(separator); //返回字符串,这个字符串将数组的每一个元素值连接在一起,中间用 separator 隔开。

var arr = ['A', 'B', 'C', 1, 2, 3];
arr.join('-'); // 'A-B-C-1-2-3'

https://www.jianshu.com/p/66b04163948b

五、js取json对象的键和值

//构建一个json对象  

var jb={"美的":49,"三星":35,"海信":25,"格力":16,"方太":14};  

for(var key in jb){  

  alert(key); //json对象的key  

  alert(jb[key]); //json对象的值  

}  

https://www.cnblogs.com/jpfss/p/9687128.html

六、js数组去重

function unique (arr) {
			  return Array.from(new Set(arr))
			}
		
		var arr = [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}];
		console.log(unique(arr));

https://segmentfault.com/a/1190000016418021?utm_source=tag-newest

七、$.each()是对数组的遍历

var arr1=['aa','bb','cc','dd'];
$.each(arr1,function(i,val){ //两个参数,第一个参数表示遍历的数组的下标,第二个参数表示下标对应的值
	 console.log(i+'```````'+val);
});

注:https://www.cnblogs.com/zhaixr/p/7069857.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值