您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 白银分类信息网,免费分类信息发布

js中遍历对象(5种)和遍历数组(6种)的方法总结

2024/3/9 3:48:39发布17次查看
本篇文章给大家带来的内容是关于js中遍历对象(5种)和遍历数组(6种)的方法总结,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
一、遍历对象方法
1.for...in
 遍历输出的是对象自身的属性以及原型链上可枚举的属性(不含symbol属性),原型链上的属性最后输出说明先遍历的是自身的可枚举属性,后遍历原型链上的
eg:var obj = { 'name': yayaya, 'age': '12', 'sex': 'female' };object.prototype.pro1 = function() {};//在原型链上添加属性object.defineproperty(obj, 'country', {  enumerable: true //可枚举});object.defineproperty(obj, 'nation', {  enumerable: false //不可枚举})obj.contry = 'china';for (var index in obj) {  console.log('key=', index, 'value=', obj[index])}
输出结果:
2.object.keys()
 遍历对象返回的是一个包含对象自身可枚举属性的数组(不含symbol属性).
eg:var obj = { 'name': yayaya, 'age': '12', 'sex': 'female' };object.prototype.pro1 = function() {}object.defineproperty(obj, 'country', {  enumerable: true,  value: 'ccc'});object.defineproperty(obj, 'nation', {  enumerable: false //不可枚举})obj.contry = 'china';object.keys(obj).foreach(function(index) {  console.log(index, obj[index])});
输出结果:
3.objcet.getownpropertynames()
输出对象自身的可枚举和不可枚举属性的数组,不输出原型链上的属性
eg:var obj = { 'name': yayaya, 'age': '12', 'sex': 'female' };object.prototype.pro1 = function() {}object.defineproperty(obj, 'country', {  enumerable: true,  value: 'ccc'});object.defineproperty(obj, 'nation', {  enumerable: false //不可枚举})obj.contry = 'china';object.getownpropertynames(obj).foreach(function(index) {  console.log(index, obj[index])});
输出结果:
4.reflect.ownkeys()
  返回对象自身的所有属性,不管属性名是symbol或字符串,也不管是否可枚举.
eg:var obj = { 'name': yayaya, 'age': '12', 'sex': 'female' };object.prototype.pro1 = function() {}object.defineproperty(obj, 'country', {  enumerable: true,  value: 'ccc'});object.defineproperty(obj, 'nation', {  enumerable: false //不可枚举})obj.contry = 'china';reflect.ownkeys(obj).foreach(function(index) {  console.log(index, obj[index])});
返回结果:
5. _.keys
用underscore插件的遍历方法只可以遍历出对象自身的可枚举属性
eg:var obj = { 'name': yayaya, 'age': '12', 'sex': 'female' };object.prototype.pro1 = function() {}object.defineproperty(obj, 'country', {  enumerable: true,  value: 'ccc'});object.defineproperty(obj, 'nation', {  enumerable: false //不可枚举})obj.contry = 'china';console.log(_.keys(obj));
输出结果:
二.遍历数组方法1.foreach
eg:var arr = ['a', 'b', 'c', 'd'];arr.foreach(function(value, index) {  console.log('value=', value, 'index=', index);})
输出结果:
2.map
可以对遍历的每一项做相应的处理,返回每次函数调用的结果组成的数组
eg:var arr = ['a', 'b', 'c', 'd'];arr.map(function(item, index, array) {  console.log(item, index);})
输出结果:
3.for循环遍历
eg:var arr = ['a', 'b', 'c', 'd'];for (var i = 0; i < arr.length; i++) {  console.log(i, arr[i])}
输出结果:
4.for...in
eg:var arr = ['a', 'b', 'c', 'd'];for (var i in arr) {  console.log('index:', i, 'value:', arr[i])}
输出结果:
5.for...of(es6)
只遍历出value,不能遍历出下标,可遍历出symbol数据类型的属性,此方法作为遍历所有数据结构的统一的方法
eg:var arr = ['a', 'b', 'c', 'd'];for (var value of arr) {  console.log('value', value)}
输出结果:
6.利用underscore插件
eg:var arr = ['a', 'b', 'c', 'd'];var _ = require('underscore');_.each(arr, function(value, index, arr) {  console.log(value, index, arr)})
输出结果:
相关推荐:
js遍历数组和对象的区别及递归遍历对象、数组、属性的方法详解
js 遍历对象的属性的代码
js遍历数组的两种方法
以上就是js中遍历对象(5种)和遍历数组(6种)的方法总结的详细内容。
白银分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录