Skip to content

Commit

Permalink
Merge pull request #310 from casaper/feat/typescript-examples
Browse files Browse the repository at this point in the history
feat(typescript-examples): _.pull
  • Loading branch information
stevemao committed Jan 13, 2022
2 parents 608e544 + 149ee7c commit 5bddb71
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1054,21 +1054,33 @@ Removes all provided values from the given array using strict equality for compa

```js
// Lodash
var array = [1, 2, 3, 1, 2, 3];
const array = [1, 2, 3, 1, 2, 3];
_.pull(array, 2, 3);
console.log(array)
// output: [1, 1]
console.log(array); // output: [1, 1]
```

// Native
var array = [1, 2, 3, 1, 2, 3];
```js
// Native JS
const array = [1, 2, 3, 1, 2, 3];
function pull(arr, ...removeList){
var removeSet = new Set(removeList)
return arr.filter(function(el){
return !removeSet.has(el)
})
}
console.log(pull(array, 2, 3))
// output: [1, 1]
console.log(pull(array, 2, 3)); // output: [1, 1]
console.log(array); // still [1, 2, 3, 1, 2, 3]. This is not in place, unlike lodash!
```

```ts
// TypeScript
const array = [1, 2, 3, 1, 2, 3];
const pull = <T extends unknown>(sourceArray: T[], ...removeList: T[]): T[] => {
const removeSet = new Set(removeList);
return sourceArray.filter(el => !removeSet.has(el));
};
console.log(pull(array, 2, 3)); // output: [1, 1]
console.log(array); // still [1, 2, 3, 1, 2, 3]. This is not in place, unlike lodash!
```

#### Browser Support for `Array.prototype.filter()`
Expand Down

0 comments on commit 5bddb71

Please sign in to comment.