Advertisement
Guest User

Untitled

a guest
Jan 25th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1.  
  2. Array.list = (function(hasOwnProperty, toString) {
  3. return function(obj, varName, expression, optFilter) {
  4. expression = new Function(varName, 'return ' + expression);
  5. if (optFilter) {
  6. optFilter = new Function(varName, 'return ' + optFilter);
  7. }
  8. var ret = [];
  9. function process(key, obj) {
  10. var value = obj[key];
  11. if (!optFilter || optFilter.call(obj, value)) {
  12. ret.push(expression.call(obj, value));
  13. }
  14. }
  15. if (toString.call(obj) == '[object Array]') {
  16. for (var i = 0, len = obj.length; i < len; i++) {
  17. process(i, obj);
  18. }
  19. }
  20. else {
  21. for (var key in obj) {
  22. if (hasOwnProperty.call(obj, key)) {
  23. process(key, obj);
  24. }
  25. }
  26. }
  27. return ret;
  28. };
  29. })({}.hasOwnProperty, {}.toString);
  30. // Array.list([1,2,3,4,5], 'x', 'x*x');
  31. // Array.list({a:'A', b:'B', c:'C'}, 'c', 'c');
  32. // Array.list([1,2,3,4,5], 'i', 'String.fromCharCode(64+i)', 'i%2');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement