Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Array.list = (function(hasOwnProperty, toString) {
- return function(obj, varName, expression, optFilter) {
- expression = new Function(varName, 'return ' + expression);
- if (optFilter) {
- optFilter = new Function(varName, 'return ' + optFilter);
- }
- var ret = [];
- function process(key, obj) {
- var value = obj[key];
- if (!optFilter || optFilter.call(obj, value)) {
- ret.push(expression.call(obj, value));
- }
- }
- if (toString.call(obj) == '[object Array]') {
- for (var i = 0, len = obj.length; i < len; i++) {
- process(i, obj);
- }
- }
- else {
- for (var key in obj) {
- if (hasOwnProperty.call(obj, key)) {
- process(key, obj);
- }
- }
- }
- return ret;
- };
- })({}.hasOwnProperty, {}.toString);
- // Array.list([1,2,3,4,5], 'x', 'x*x');
- // Array.list({a:'A', b:'B', c:'C'}, 'c', 'c');
- // Array.list([1,2,3,4,5], 'i', 'String.fromCharCode(64+i)', 'i%2');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement