Selaa lähdekoodia

added object helper methods

mightyplow 8 vuotta sitten
vanhempi
commit
2fe9b9fa90
1 muutettua tiedostoa jossa 19 lisäystä ja 0 poistoa
  1. 19 0
      lib/object.js

+ 19 - 0
lib/object.js

@@ -9,5 +9,24 @@ module.exports = {
                 obj[key] = values[key]
             }
         })
+    },
+
+    forEach: (obj, callback) => {
+        Object.keys(obj).forEach(key => {
+            callback(key, obj[key])
+        })
+    },
+
+    reduce: (obj, fnReduce, startValue) => {
+        return Object.keys(obj).reduce((acc, key) => {
+            return fnReduce.call(null, acc, key, obj[key])
+        }, startValue)
+    },
+
+    map: (obj, fnMap) => {
+        return Object.keys(obj).reduce((mapped, key) => {
+            mapped[key] = fnMap(obj[key], key)
+            return mapped
+        }, {})
     }
 }