js method traverse the object (5 species) and iterate (six kinds) summary (reproduced)

A traversing object method

1.for ... in
traversal is outputted on the object itself and the properties of the prototype chain enumerable properties (excluding Symbol properties), properties of the final output instructions on the first prototype chain traversal is itself can be enumerated attribute, after traversing the prototype chain

eg:

var obj = { 'name': "yayaya", 'Age': '12 is', 'Sex': 'FEMALE' }; 
Object.prototype.pro1 = function () {}; // add properties on prototype chain 
Object .defineProperty (obj, 'Country' , { 
  Enumerable: to true  // enumerable 
}); 
Object.defineProperty (obj, 'Nation' , { 
  Enumerable: to false  // not enumerable 
}) 
obj.contry = 'China' ;
 for ( var index in obj) { 
  the console.log ( 'Key =', index, 'value =' ,obj[index])
}

Output:

name = value = yayaya Key
Key = value Age = 12 is
Key value = Sex = FEMALE
Key = value = Contry China
Key = Pro1 value = function () {}

2.Object.keys ()
traverse the object returns an object itself comprising can be enumerated attribute array (excluding Symbol attributes).

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])
});

Output:

yayaya name
Age 12 is
Sex FEMALE
Contry China

3.Objcet.getOwnPropertyNames ()
output enumeration and the object itself can not be enumerated attribute array, the output attribute is not prototype chain

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])
});

Output:
name yayaya
Age 12
Sex FEMALE
Country ccc
Nation undefined
Contry china

4.Reflect.ownKeys ()
returns the object of all their property, whether the property name is Symbol or string, whether or not enumerable.

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])
});

Returns the result:
name yayaya
Age 12 is
Sex FEMALE
Country CCC
Nation undefined
Contry China

5. The _.keys
by underscore widget traversal method can only traverse the object itself may be enumerated attribute

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));

Output:

Sex Age Country name

. iterate two methods
1.forEach

EG:

var arr = ['a', 'b', 'c', 'd'];
arr.forEach(function(value, index) {
    console.log('value=', value, 'index=', index);
})

Output:

index = 0 A = value
value = B. 1 = index
value = 2 = C index
value index = D =. 3

2.map
can each corresponding processing traversal, the results of each function call returns an array

eg :

var arr = ['a', 'b', 'c', 'd'];
arr.map(function(item, index, array) {
    console.log(item, index);
})

Output:

0 A
B. 1
C 2
D. 3

3.for loop through

eg:

var arr = ['a', 'b', 'c', 'd'];
for (var i = 0; i < arr.length; i++) {
    console.log(i, arr[i])
}

Output:

0 a
1 b
2 c
3 d

4.For ... in

eg:

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

Output:

index: 0 value: A
index: value. 1: B
index: 2 value: C
index: value. 3: D

5.for ... of (ES6)
traverses only a value, can not traverse the index, the data may traverse the Symbol type attribute, this method as a method for traversing data structures unify all

eg:

var arr = ['a', 'b', 'c', 'd'];
for (var value of arr) {
    console.log('value', value)
}

Output:

value a
value b
value c
value d

6.利用underscore插件

eg:

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

Output:

a 0 ['a','b','c',''d]
b 1 ['a','b','c',''d]
c 2 ['a','b','c',''d]
d 3 ['a','b','c',''d]

Guess you like

Origin www.cnblogs.com/joe235/p/11585858.html