首页 > web前端 > js教程 > 正文

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

不言
发布: 2018-08-15 11:56:57
原创
60432 人浏览过

本篇文章给大家带来的内容是关于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])
}
登录后复制

输出结果:

799983786-5b729a263c64e_articlex.png

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])
});
登录后复制

输出结果:

3916344265-5b72a0095bbbe_articlex.png

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])
});
登录后复制

输出结果:

3535806854-5b729fb25511d_articlex.png

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])
});
登录后复制

返回结果:

1872847963-5b72a4e658d22_articlex.png

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));
登录后复制

输出结果:

1276851218-5b7390da2e4f8_articlex.png

二.遍历数组方法

1.forEach

eg:
var arr = ['a', 'b', 'c', 'd'];
arr.forEach(function(value, index) {
  console.log('value=', value, 'index=', index);
})
登录后复制

输出结果:

335831390-5b7382004aa9b_articlex.png

2.map
可以对遍历的每一项做相应的处理,返回每次函数调用的结果组成的数组

eg:
var arr = ['a', 'b', 'c', 'd'];
arr.map(function(item, index, array) {
  console.log(item, index);
})
登录后复制

输出结果:

2569786942-5b738b494d722_articlex.png

3.for循环遍历

eg:
var arr = ['a', 'b', 'c', 'd'];
for (var i = 0; i < arr.length; i++) {
  console.log(i, arr[i])
}
登录后复制

输出结果:

314358687-5b738923bff94_articlex.png

4.for...in

eg:
var arr = ['a', 'b', 'c', 'd'];
for (var i in arr) {
  console.log('index:', i, 'value:', arr[i])
}
登录后复制

输出结果:

707618129-5b738ce80cefa_articlex.png

5.for...of(es6)
只遍历出value,不能遍历出下标,可遍历出Symbol数据类型的属性,此方法作为遍历所有数据结构的统一的方法

eg:
var arr = ['a', 'b', 'c', 'd'];
for (var value of arr) {
  console.log('value', value)
}
登录后复制

输出结果:

1197965378-5b738d5e4965b_articlex.png

6.利用underscore插件

eg:
var arr = ['a', 'b', 'c', 'd'];
var _ = require('underscore');
_.each(arr, function(value, index, arr) {
  console.log(value, index, arr)

})
登录后复制

输出结果:

2760979425-5b738ffd73110_articlex.png

相关推荐:

JS遍历数组和对象的区别及递归遍历对象、数组、属性的方法详解

js 遍历对象的属性的代码

js遍历数组的两种方法

以上是js中遍历对象(5种)和遍历数组(6种)的方法总结的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!