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

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

}

输出结果:

0c47552b5fd13d0f0d014fface5fc965.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])

});

输出结果:

e4511b4edba5cb42d7fd439ce74cb80e.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])

});

输出结果:

5617771440ba633fcdc485d05f1bdc41.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])

});

返回结果:

00a6e6d90626bc6d10338d7b7dd5cedc.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));

输出结果:

e57f2e9e31b05f2c8495e1b797374660.png

二.遍历数组方法

1.forEacheg:

var arr = ['a', 'b', 'c', 'd'];

arr.forEach(function(value, index) {

console.log('value=', value, 'index=', index);

})

输出结果:

087b628c26344e3614614f8ef04332aa.png

2.map

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

var arr = ['a', 'b', 'c', 'd'];

arr.map(function(item, index, array) {

console.log(item, index);

})

输出结果:

f03d97b2bab6eae7642df6057ad721f5.png

3.for循环遍历eg:

var arr = ['a', 'b', 'c', 'd'];

for (var i = 0; i < arr.length; i++) {

console.log(i, arr[i])

}

输出结果:

2c1abaa9a3ca4f57b9a7cd44be6f73d3.png

4.for...ineg:

var arr = ['a', 'b', 'c', 'd'];

for (var i in arr) {

console.log('index:', i, 'value:', arr[i])

}

输出结果:

8c943d643ad95151e8ade0a9bf08d56e.png

5.for...of(es6)

只遍历出value,不能遍历出下标,可遍历出Symbol数据类型的属性,此方法作为遍历所有数据结构的统一的方法eg:

var arr = ['a', 'b', 'c', 'd'];

for (var value of arr) {

console.log('value', value)

}

输出结果:

ebd3e5fb57189cead1c2b721af0fac00.png

6.利用underscore插件eg:

var arr = ['a', 'b', 'c', 'd'];

var _ = require('underscore');

_.each(arr, function(value, index, arr) {

console.log(value, index, arr)

})

输出结果:

22e351558c1899ad0f085ee59dea796c.png

相关推荐:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值