博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通过一个例子,总结下检测数组属性的N种方法
阅读量:6902 次
发布时间:2019-06-27

本文共 1533 字,大约阅读时间需要 5 分钟。

判断arr数组里是否含有a,有a返回1;没有返回2 var arr = [{a:1,b:2,c:3},{q:1,w:2,e:3},{s:4,g:5,i:9},{b:2,v:3,u:4}];

  检测属性的3种方法:1、in运算符 2、hasOwnProperty()  3、!= underfind

1、用hasOwnProperty()  结合 for()、$.each()、array.forEach()等方法

var arr = [{a:1,b:2,c:3},{q:1,w:2,e:3},{s:4,g:5,i:9},{b:2,v:3,u:4}];for(var i=0;i
jQuery.each()函数用于遍历指定的对象和数组$.each( object, callback ) Object类型指定需要遍历的对象或数组callback  Function类型 指定的用于循环执行的函数 var arr = [{a:1,b:2,c:3},{q:1,w:2,e:3},{s:4,g:5,i:9},{b:2,v:3,u:4}];   $.each(arr,function(index,item){       if(item.hasOwnProperty("a")){            console.log(1);     }else{        console.log(2);    }    或者用for in
$.each(arr,function(index,item){
for(i in item){
if(i=="a"){
console.log(1); }else{
console.log(2); } } }) 或者用 != underfind
$.each(arr,function(index,item){
if(item.a != undefined){
console.log(1); }else{
console.log(2); } })
 
forEach()方法用于调用数组的每个元素,并将元素传递给回调函数。语法:array.forEach(function(currentValue,index,arr),thisValue); var arr = [{a:1,b:2,c:3},{q:1,w:2,e:3},{s:4,g:5,i:9},{b:2,v:3,u:4}];arr.forEach(function(index,item){     if(item.hasOwnProperty("a"){         console.log(1);    }else{      console.log(2);    }})

  2、for in 结合 !=underfind

var arr = [{a:1,b:2,c:3},{q:1,w:2,e:3},{s:4,g:5,i:9},{b:2,v:3,u:4}];for(o in arr){  if(arr[o].a !=underfind){    console.log(1);  }else{     console.log(2);   }}

  

  

  

转载于:https://www.cnblogs.com/colorful-paopao1/p/9076959.html

你可能感兴趣的文章
Mozilla 宣布关闭 Persona
查看>>
Ubuntu和FreeBSD即将合体:UbuntuBSD
查看>>
漏洞预警:GitLab 权限泄露漏洞
查看>>
攻击者开始利用 ImageMagick 漏洞攻击网站
查看>>
《Java多线程编程核心技术》——1.12节本章小结
查看>>
看,那人好像一个产品狗,对,这就是产品狗
查看>>
《Visual Basic 2012入门经典》----2.3 使用工具栏
查看>>
《Nmap渗透测试指南》—第10章10.7节Scans标签
查看>>
Machine Learning in Action -- AdaBoost
查看>>
无等待是不够的
查看>>
在 Apache Hive 中轻松生存的12个技巧
查看>>
《树莓派开发实战(第2版)》——2.11 在Mac上共享树莓派的屏幕
查看>>
Scala 泛型
查看>>
API时代,每个人都能拥有阿凡达,天了撸!
查看>>
《制造业中的机器人、自动化和系统集成》—— 3.6 装配自动化组件
查看>>
《 Java并发编程从入门到精通》 Java线程池的监控
查看>>
用深度学习来获取文本语义: 词向量应用于自然语言处理
查看>>
《树莓派渗透测试实战》——2.9 通过SSH做反向Shell
查看>>
《NoSQL权威指南》——2.5 执行一个ALTER语句
查看>>
《Ansible权威指南》一1.8 Python多环境扩展管理
查看>>