From e8293d89de4a515af9d02432254e1a858db16290 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 15 Dec 2017 13:06:53 +0200 Subject: [PATCH] Tag, lint, build --- README.md | 14 ++++++++++++++ snippets/array-includes.md | 6 +++--- tag_database | 1 + 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b8412a802..416266397 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ ### Array * [Array concatenation](#array-concatenation) * [Array difference](#array-difference) +* [Array includes](#array-includes) * [Array intersection](#array-intersection) * [Array sample](#array-sample) * [Array union](#array-union) @@ -142,6 +143,19 @@ const difference = (a, b) => { const s = new Set(b); return a.filter(x => !s.has [⬆ back to top](#table-of-contents) +### Array includes + +Use `slice()` to offset the array/string and `indexOf()` to check if the value is included. +Omit the last argument, `fromIndex`, to check the whole array/string. + +```js +const includes = (collection, val, fromIndex=0) => collection.slice(fromIndex).indexOf(val) != -1; +// includes("30-seconds-of-code", "code") -> true +// includes([1, 2, 3, 4], [1, 2], 1) -> false +``` + +[⬆ back to top](#table-of-contents) + ### Array intersection Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values contained in `b`. diff --git a/snippets/array-includes.md b/snippets/array-includes.md index c068d17b2..5ebcbc009 100644 --- a/snippets/array-includes.md +++ b/snippets/array-includes.md @@ -1,10 +1,10 @@ ### Array includes -Use `slice()` to offset the array/string. `Array.indexOf()` returns `-1` if the sub-string/array dosen't contain the given `value`. +Use `slice()` to offset the array/string and `indexOf()` to check if the value is included. +Omit the last argument, `fromIndex`, to check the whole array/string. ```js const includes = (collection, val, fromIndex=0) => collection.slice(fromIndex).indexOf(val) != -1; - // includes("30-seconds-of-code", "code") -> true // includes([1, 2, 3, 4], [1, 2], 1) -> false -``` \ No newline at end of file +``` diff --git a/tag_database b/tag_database index 7e20b7516..67bdedd3c 100644 --- a/tag_database +++ b/tag_database @@ -1,6 +1,7 @@ anagrams-of-string-(with-duplicates):string array-concatenation:array array-difference:array +array-includes:array array-intersection:array array-sample:array array-union:array