Add forOwn and forOwnRight

This commit is contained in:
Angelos Chalaris
2018-01-18 16:45:56 +02:00
parent c07cd0dc36
commit ebe30f4c80
3 changed files with 28 additions and 0 deletions

13
snippets/forOwn.md Normal file
View File

@ -0,0 +1,13 @@
### forOwn
Iterates over all own properties of an object, running a callback for each one.
Use `Object.keys(obj)` to get all the properties of the object, `Array.forEach()` to run the provided function for each key-value pair. The callback receives three arguments - the value, the key and the object.
```js
const forOwn = (obj, fn) => Object.keys(obj).forEach(key => fn(obj[key],key,obj));
```
```js
forOwn({foo: 'bar', a: 1}, v => console.log(v)); // 'bar', 1
```

13
snippets/forOwnRight.md Normal file
View File

@ -0,0 +1,13 @@
### forOwnRight
Iterates over all own properties of an object in reverse, running a callback for each one.
Use `Object.keys(obj)` to get all the properties of the object, `Array.reverse()` to reverse their order and `Array.forEach()` to run the provided function for each key-value pair. The callback receives three arguments - the value, the key and the object.
```js
const forOwnRight = (obj, fn) => Object.keys(obj).reverse().forEach(key => fn(obj[key],key,obj));
```
```js
forOwnRight({foo: 'bar', a: 1}, v => console.log(v)); // 1, 'bar'
```

View File

@ -52,6 +52,8 @@ flatten:array
flip:adapter,function
forEachRight:array,function
formatDuration:date,math,string,utility
forOwn:object
forOwnRight:object
fromCamelCase:string
functionName:function,utility
functions:object,function