forked from podgorniy/javascript-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterate.js
More file actions
46 lines (36 loc) · 754 Bytes
/
Copy pathiterate.js
File metadata and controls
46 lines (36 loc) · 754 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
exaple of use
var data, processed;
data = [10,20,30,40];
processed = iterate(data, function (val, i) {
console.log(val);
});
data = {
masha : 10,
pasha : 20,
sasha : 30,
lesha : 40
};
processed = iterate(data, function (val, i) {
console.log(val);
});
*/
// THINK same returning type. Like "new list.constructor"
function iterate(list, func) {
'use strict';
var i, res;
if ('length' in list && typeof list !== 'function') {
res = [];
for (i = 0; i < list.length; i += 1) {
res.push(func.call(list, list[i], i));
}
} else {
res = {};
for (i in list) {
if (list.hasOwnProperty(i)) {
res[i] = func.call(list, list[i], i);
}
}
}
return res;
}