From a7790db34c09ce4ef7e0d6cf2e70e53c6952ad6d Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 18 Jan 2018 16:45:56 +0200 Subject: [PATCH] Add forOwn and forOwnRight --- snippets/forOwn.md | 13 +++++++++++++ snippets/forOwnRight.md | 13 +++++++++++++ tag_database | 2 ++ 3 files changed, 28 insertions(+) create mode 100644 snippets/forOwn.md create mode 100644 snippets/forOwnRight.md diff --git a/snippets/forOwn.md b/snippets/forOwn.md new file mode 100644 index 000000000..7618f4618 --- /dev/null +++ b/snippets/forOwn.md @@ -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 +``` diff --git a/snippets/forOwnRight.md b/snippets/forOwnRight.md new file mode 100644 index 000000000..7a85c9d15 --- /dev/null +++ b/snippets/forOwnRight.md @@ -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' +``` diff --git a/tag_database b/tag_database index fe9a9ef38..3930b5dc8 100644 --- a/tag_database +++ b/tag_database @@ -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