Skip to content

Commit

Permalink
Object form of _.forEach
Browse files Browse the repository at this point in the history
  • Loading branch information
joshkel committed Aug 16, 2024
1 parent a66d611 commit 6e28647
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 18 deletions.
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ Please read [how to contribute](./src/content/how-to-contribute.md) if you want
<a href="https://github.com/102"><img src="https://avatars.githubusercontent.com/u/5839225?v=4" title="102" width="80" height="80"></a>
<a href="https://github.com/sirLisko"><img src="https://avatars.githubusercontent.com/u/435399?v=4" title="sirLisko" width="80" height="80"></a>
<a href="https://github.com/codedbypaul"><img src="https://avatars.githubusercontent.com/u/4680439?v=4" title="codedbypaul" width="80" height="80"></a>
<a href="https://github.com/Adrian-Benavente"><img src="https://avatars.githubusercontent.com/u/11258517?v=4" title="Adrian-Benavente" width="80" height="80"></a>
<a href="https://github.com/ajitfawade"><img src="https://avatars.githubusercontent.com/u/16475300?v=4" title="ajitfawade" width="80" height="80"></a>
<a href="https://github.com/slimcandy"><img src="https://avatars.githubusercontent.com/u/91788772?v=4" title="slimcandy" width="80" height="80"></a>
<a href="https://github.com/BrettMerri"><img src="https://avatars.githubusercontent.com/u/4353965?v=4" title="BrettMerri" width="80" height="80"></a>
<a href="https://github.com/ColinRTaylor"><img src="https://avatars.githubusercontent.com/u/11527179?v=4" title="ColinRTaylor" width="80" height="80"></a>
<a href="https://github.com/fgandellini"><img src="https://avatars.githubusercontent.com/u/971375?v=4" title="fgandellini" width="80" height="80"></a>
<a href="https://github.com/friederbluemle"><img src="https://avatars.githubusercontent.com/u/743291?v=4" title="friederbluemle" width="80" height="80"></a>
<a href="https://github.com/harshit-sinha-developer"><img src="https://avatars.githubusercontent.com/u/12970743?v=4" title="harshit-sinha-developer" width="80" height="80"></a>
<a href="https://github.com/jeromelachaud"><img src="https://avatars.githubusercontent.com/u/456474?v=4" title="jeromelachaud" width="80" height="80"></a>
<a href="https://github.com/jpbriggs408"><img src="https://avatars.githubusercontent.com/u/8880358?v=4" title="jpbriggs408" width="80" height="80"></a>
<a href="https://github.com/martingjaldbaek"><img src="https://avatars.githubusercontent.com/u/752617?v=4" title="martingjaldbaek" width="80" height="80"></a>
<a href="https://github.com/rouanw"><img src="https://avatars.githubusercontent.com/u/2362668?v=4" title="rouanw" width="80" height="80"></a>
<a href="https://github.com/SgtPooki"><img src="https://avatars.githubusercontent.com/u/1173416?v=4" title="SgtPooki" width="80" height="80"></a>
<a href="https://github.com/ryanosaur"><img src="https://avatars.githubusercontent.com/u/5642293?v=4" title="ryanosaur" width="80" height="80"></a>
<a href="https://github.com/shubhamV123"><img src="https://avatars.githubusercontent.com/u/29898106?v=4" title="shubhamV123" width="80" height="80"></a>
<a href="https://github.com/tombiju"><img src="https://avatars.githubusercontent.com/u/11186044?v=4" title="tombiju" width="80" height="80"></a>
<a href="https://github.com/haykam821"><img src="https://avatars.githubusercontent.com/u/24855774?v=4" title="haykam821" width="80" height="80"></a>
<a href="https://github.com/tombiju"><img src="https://avatars.githubusercontent.com/u/11186044?v=4" title="tombiju" width="80" height="80"></a>
<a href="https://github.com/shubhamV123"><img src="https://avatars.githubusercontent.com/u/29898106?v=4" title="shubhamV123" width="80" height="80"></a>
<a href="https://github.com/ryanosaur"><img src="https://avatars.githubusercontent.com/u/5642293?v=4" title="ryanosaur" width="80" height="80"></a>
<a href="https://github.com/SgtPooki"><img src="https://avatars.githubusercontent.com/u/1173416?v=4" title="SgtPooki" width="80" height="80"></a>
<a href="https://github.com/rouanw"><img src="https://avatars.githubusercontent.com/u/2362668?v=4" title="rouanw" width="80" height="80"></a>
<a href="https://github.com/martingjaldbaek"><img src="https://avatars.githubusercontent.com/u/752617?v=4" title="martingjaldbaek" width="80" height="80"></a>
<a href="https://github.com/jpbriggs408"><img src="https://avatars.githubusercontent.com/u/8880358?v=4" title="jpbriggs408" width="80" height="80"></a>
<a href="https://github.com/jeromelachaud"><img src="https://avatars.githubusercontent.com/u/456474?v=4" title="jeromelachaud" width="80" height="80"></a>
<a href="https://github.com/harshit-sinha-developer"><img src="https://avatars.githubusercontent.com/u/12970743?v=4" title="harshit-sinha-developer" width="80" height="80"></a>
<a href="https://github.com/friederbluemle"><img src="https://avatars.githubusercontent.com/u/743291?v=4" title="friederbluemle" width="80" height="80"></a>
<a href="https://github.com/fgandellini"><img src="https://avatars.githubusercontent.com/u/971375?v=4" title="fgandellini" width="80" height="80"></a>
<a href="https://github.com/ColinRTaylor"><img src="https://avatars.githubusercontent.com/u/11527179?v=4" title="ColinRTaylor" width="80" height="80"></a>
<a href="https://github.com/BrettMerri"><img src="https://avatars.githubusercontent.com/u/4353965?v=4" title="BrettMerri" width="80" height="80"></a>
<a href="https://github.com/slimcandy"><img src="https://avatars.githubusercontent.com/u/91788772?v=4" title="slimcandy" width="80" height="80"></a>
<a href="https://github.com/ajitfawade"><img src="https://avatars.githubusercontent.com/u/16475300?v=4" title="ajitfawade" width="80" height="80"></a>
<a href="https://github.com/Adrian-Benavente"><img src="https://avatars.githubusercontent.com/u/11258517?v=4" title="Adrian-Benavente" width="80" height="80"></a>
<a href="https://github.com/doydoy44"><img src="https://avatars.githubusercontent.com/u/6697165?v=4" title="doydoy44" width="80" height="80"></a>

[//]: contributor-faces
7 changes: 7 additions & 0 deletions src/content/lodash/collection/forEach/lodash.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ forEach(array, (item, index) => {
// => a 0
// => b 1
// => c 2

const obj = { a: 'red', b: 'green' }
forEach(obj, (item, key) => {
console.log(item, key)
})
// => red a
// => green b
2 changes: 1 addition & 1 deletion src/content/lodash/collection/forEach/notes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Iterates over elements of <code>collection</code> and invokes <code>iteratee</code> for each element. The iteratee is invoked with three arguments: <em>(value, index|key, collection)</em>. Iteratee functions may exit iteration early by explicitly returning <code>false</code>.

Resources: [Array#forEach](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) [Source](https://www.reindex.io/blog/you-might-not-need-underscore/#iterate)
Resources: [Array#forEach](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) [Object#entries](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries) [Source](https://www.reindex.io/blog/you-might-not-need-underscore/#iterate)
4 changes: 4 additions & 0 deletions src/content/lodash/collection/forEach/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ describe('forEach', () => {
expect(console.log).toHaveBeenNthCalledWith(1, 'a', 0)
expect(console.log).toHaveBeenNthCalledWith(2, 'b', 1)
expect(console.log).toHaveBeenNthCalledWith(3, 'c', 2)
expect(console.log).toHaveBeenNthCalledWith(4, 'red', 'a')
expect(console.log).toHaveBeenNthCalledWith(5, 'green', 'b')
})
})

Expand All @@ -32,6 +34,8 @@ describe('forEach', () => {
expect(console.log).toHaveBeenNthCalledWith(1, 'a', 0)
expect(console.log).toHaveBeenNthCalledWith(2, 'b', 1)
expect(console.log).toHaveBeenNthCalledWith(3, 'c', 2)
expect(console.log).toHaveBeenNthCalledWith(4, 'red', 'a')
expect(console.log).toHaveBeenNthCalledWith(5, 'green', 'b')
})
})
})
8 changes: 7 additions & 1 deletion src/content/lodash/collection/forEach/vanilla.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
;['a', 'b', 'c'].forEach((item, index) => {
['a', 'b', 'c'].forEach((item, index) => {
console.log(item, index)
})
// => a 0
// => b 1
// => c 2

Object.entries({ a: 'red', b: 'green' }).forEach(([key, item]) => {
console.log(item, key)
})
// => red a
// => green b

0 comments on commit 6e28647

Please sign in to comment.