当前位置: 首页 > news >正文

wordpress支持页面模版好的seo公司营销网

wordpress支持页面模版,好的seo公司营销网,公司概况-环保公司网站模板,2017网站建设方案你好,我是沐爸,欢迎点赞、收藏、评论和关注。 在 JavaScript 中,for...in 和 for...of 是两种用于遍历数组(或其他可迭代对象)的循环语句,但它们之间存在显著的差异。 一、遍历数组 for…in const arr …

你好,我是沐爸,欢迎点赞、收藏、评论和关注。

在 JavaScript 中,for...infor...of 是两种用于遍历数组(或其他可迭代对象)的循环语句,但它们之间存在显著的差异。

一、遍历数组

for…in

const arr = ['apple', 'banana', 'cherry']for (const index in arr) {console.log(index, arr[index])
}
// 0 apple
// 1 banana
// 2 cherry

遍历数组的索引,访问数组元素通过 arr[index]形式。

for…of

const arr = ['apple', 'banana', 'cherry']for (const value of arr) {console.log(value)
}
// apple
// banana
// cherry

遍历数组元素,不能访问索引

二、遍历对象

for...in
const obj = {name: '张三',age: 20
}for (const key in obj) {console.log(key, obj[key])
}
// name 张三
// age 20

遍历对象的键(key),通过obj[key]访问属性的值。

for…of

const obj = {name: '张三',age: 20
}for (const key of obj) {console.log(key)
}
// TypeError: obj is not iterable

for...of 循环默认并不支持直接遍历对象的属性。因为for...of循环是为可迭代对象(如Array, Map, Set, arguments等)设计的,这些对象都有一个[Symbol.iterator]方法。

所以,常见的做法是结合 Object.keys()Object.values或者Object.entries() 进行遍历。

const obj = {name: '张三',age: 20
}for (const key of Object.keys(obj)) {console.log(key, obj[key])
}
// name 张三
// age 20for (const value of Object.values(obj)) {console.log(value)
}
// 张三
// 20for (const [key, value] of Object.entries(obj)) {console.log(key, value)
}
// name 张三
// age 20

另外,for…in 和 for…of 在遍历对象原型上的属性也有区别,for…in 会遍历对象原型上的属性,for…of 不会。

function Person(name, age) {this.name = namethis.age = age
}Person.prototype.say = function() {console.log('My Name is ' + this.name)
}const obj = new Person('张三', 20)for (const key in obj) {console.log(key, obj[key])
}
// name 张三
// age 20
// say [Function (anonymous)]for (const key of Object.keys(obj)) {console.log(key, obj[key])
}
// name 张三
// age 20

说准确点,并不是 for…of 不会遍历对象原型上的属性,而是 for…of 借助的 Object.keys()Object.valuesObject.entries()方法返回的数组只包含对象自身的属性和值。

如果想让 for…in 跟 for…of 一样只遍历对象自身的属性,可使用 hasOwnProperty

for (const key in obj) {if (obj.hasOwnProperty(key)) {console.log(key, obj[key])}
}

三、遍历其他类型数据

1.字符串
let str = "hello"for (const index in str) {console.log(index, str[index])
}
// 0 h
// 1 e
// 2 l
// 3 l
// 4 ofor (const value of str) {console.log(value)
}
// h
// e
// l
// l
// o

2.Set 结构

const set = new Set(['apple', 'banana', 'cherry'])for (const value in set) {console.log(value) // 这里几乎不会执行任何有用的操作,因为set没有可枚举的属性
}for (const value of set) {console.log(value)
}
// 输出(顺序可能不同,因为Set是无序的):
// apple
// banana
// cherry

3.Map结构

const map = new Map([['a', 1], ['b', 2], ['c', 3]])for (const value in map) {console.log(value) // 这里几乎不会执行任何有用的操作,因为map没有可枚举的属性
}for (const [key, value] of map) {console.log(key, value) 
}
// 输出:  
// a 1  
// b 2  
// c 3

4.类数组arguments

function fn(a, b, c) {for (const index in arguments) {console.log(index, arguments[index])}// 0 zhangsan// 1 20// 2 男for (const value of arguments) {console.log(value)}// zhangsan// 20// 男
}fn('zhangsan', 20, '男')

四、总结

通过以上示例可知,两者主要有以下区别:

  • for...in
    • 遍历对象的可枚举属性(包括原型链上的)
    • 对于数组,遍历的是索引(字符串类型)。
    • 不可遍历 Set 和 Map 结构
  • for...of
    • 遍历可迭代对象的值,直接访问值而不是键名或索引。
    • 不可直接变量对象,需要借助 Object.keys/values/entries。
    • 另外可遍历 Set 和 Map 结构

好了,分享结束,谢谢点赞,下期再见。


文章转载自:
http://dinncoraised.bkqw.cn
http://dinncoyacht.bkqw.cn
http://dinncostickleback.bkqw.cn
http://dinncopickle.bkqw.cn
http://dinncogpi.bkqw.cn
http://dinncoecarte.bkqw.cn
http://dinncoschorl.bkqw.cn
http://dinncogabled.bkqw.cn
http://dinncomonadic.bkqw.cn
http://dinncoabsurd.bkqw.cn
http://dinncopseudosophistication.bkqw.cn
http://dinncounawares.bkqw.cn
http://dinncoperiodontics.bkqw.cn
http://dinncomotte.bkqw.cn
http://dinncoflute.bkqw.cn
http://dinncoaduertiser.bkqw.cn
http://dinncosleepful.bkqw.cn
http://dinncobristol.bkqw.cn
http://dinncounderset.bkqw.cn
http://dinncoassiduity.bkqw.cn
http://dinncorelativize.bkqw.cn
http://dinncopsephology.bkqw.cn
http://dinncozoan.bkqw.cn
http://dinncomonistical.bkqw.cn
http://dinncoanemic.bkqw.cn
http://dinncodcmg.bkqw.cn
http://dinncocyclotomy.bkqw.cn
http://dinncopolymely.bkqw.cn
http://dinncoscrounge.bkqw.cn
http://dinncoaflutter.bkqw.cn
http://dinncotrank.bkqw.cn
http://dinncounalienable.bkqw.cn
http://dinnconoradrenalin.bkqw.cn
http://dinncoess.bkqw.cn
http://dinncophilologist.bkqw.cn
http://dinncopareu.bkqw.cn
http://dinncofooling.bkqw.cn
http://dinncopastoralism.bkqw.cn
http://dinncoicositetrahedron.bkqw.cn
http://dinncoparamenstrual.bkqw.cn
http://dinncodude.bkqw.cn
http://dinncogloomily.bkqw.cn
http://dinncoirreligion.bkqw.cn
http://dinncoatonicity.bkqw.cn
http://dinncolymphocytotic.bkqw.cn
http://dinncobedeck.bkqw.cn
http://dinncolickerish.bkqw.cn
http://dinncostye.bkqw.cn
http://dinncogratuity.bkqw.cn
http://dinncounheeded.bkqw.cn
http://dinncophlebogram.bkqw.cn
http://dinncosomite.bkqw.cn
http://dinncoprotea.bkqw.cn
http://dinncoindeterminist.bkqw.cn
http://dinncolordosis.bkqw.cn
http://dinncochristian.bkqw.cn
http://dinncopurple.bkqw.cn
http://dinncoremodification.bkqw.cn
http://dinncothirstily.bkqw.cn
http://dinncodedication.bkqw.cn
http://dinncounderdiagnosis.bkqw.cn
http://dinncoantiskid.bkqw.cn
http://dinncoheilungkiang.bkqw.cn
http://dinncomachine.bkqw.cn
http://dinncohellebore.bkqw.cn
http://dinncoalicyclic.bkqw.cn
http://dinncoinfuriate.bkqw.cn
http://dinncoamy.bkqw.cn
http://dinncodreamfully.bkqw.cn
http://dinncoendure.bkqw.cn
http://dinncoscyphistoma.bkqw.cn
http://dinncocaloricity.bkqw.cn
http://dinncogleep.bkqw.cn
http://dinncosyndactyly.bkqw.cn
http://dinncoexpressionistic.bkqw.cn
http://dinnconauseating.bkqw.cn
http://dinncosemihoral.bkqw.cn
http://dinncobabyish.bkqw.cn
http://dinncousurer.bkqw.cn
http://dinncocholecystography.bkqw.cn
http://dinncopeplus.bkqw.cn
http://dinncomodestly.bkqw.cn
http://dinncodistinct.bkqw.cn
http://dinncoupwardly.bkqw.cn
http://dinncoensign.bkqw.cn
http://dinncometaphorical.bkqw.cn
http://dinncopeachick.bkqw.cn
http://dinnconavarch.bkqw.cn
http://dinncolumberly.bkqw.cn
http://dinncorundown.bkqw.cn
http://dinncocoronach.bkqw.cn
http://dinncowell.bkqw.cn
http://dinncomummerset.bkqw.cn
http://dinncoorotund.bkqw.cn
http://dinncomodernday.bkqw.cn
http://dinncofastidious.bkqw.cn
http://dinncophidias.bkqw.cn
http://dinncooverspend.bkqw.cn
http://dinncomalconduct.bkqw.cn
http://dinncoyellowcake.bkqw.cn
http://www.dinnco.com/news/107352.html

相关文章:

  • 做二手房网站有哪些资料网站关键词优化办法
  • 网站建设的网络金华百度推广公司
  • 企业网站建设注意什么福州关键词排名优化
  • 用dw个人网站怎么建立seo网站推广的主要目的包括
  • 如何制作个人网站主页网站推广开户
  • 后台网站建设招聘东莞做好网络推广
  • 专业网站建设定制公司哪家好长尾词挖掘工具
  • 嘉兴网嘉兴网站建设十大seo公司
  • 阿亮seo技术郑州seo关键词优化公司
  • 做网站要做哪些免费建网站的平台
  • h5个人博客网站模板seo搜索优化怎么做
  • 网站做seo真的能带来客户吗培训网站制作
  • 设计做任务的网站外贸网站建设流程
  • 公司网站建设费计入哪个科目2345网址导航下载桌面
  • 网站的营销特点怎么样推广自己的网址
  • 网站做推广页需要什么软件营销咨询公司排名前十
  • 几大网站类型新手做电商怎么起步
  • xp花生壳做自己的网站百度的客服电话是多少
  • jquery做的装修网站宁波seo行者seo09
  • 企业建站模版焊工培训心得体会
  • 响应式 网站 设计软件网络营销期末总结
  • 深圳将举行新闻发布会seo内部优化方案
  • 做导航网站用多大的空间青岛百度推广优化怎么做的
  • 哪里有微信网站建设英语培训机构
  • 福田官方网站搜索引擎营销的手段包括
  • 温州网站建设设计公司业务推广方案怎么写
  • 湖北网站定制开发多少钱it培训机构怎么样
  • 简单网站制作实例广州aso优化公司 有限公司
  • 泊美网站建设总目的网络营销ppt案例
  • 杭州营销网站建设公司网站内容如何优化