Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bloqhead/jsbasics
A library of reusable approaches for various things in JS.
https://github.com/bloqhead/jsbasics
Last synced: 3 days ago
JSON representation
A library of reusable approaches for various things in JS.
- Host: GitHub
- URL: https://github.com/bloqhead/jsbasics
- Owner: bloqhead
- Created: 2020-07-23T14:32:45.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-09-18T02:33:50.000Z (over 4 years ago)
- Last Synced: 2024-11-15T06:35:52.913Z (2 months ago)
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# JS Basics
### Objects
Removing values from an object in a non-destructive manner:
``` js
const myObject = {
a: 1,
b: 2,
c: 3
};
const { a, ...noA } = myObject;
console.log(noA); // => { b: 2, c: 3 }
```_Source: [codeburst.io](https://codeburst.io/use-es2015-object-rest-operator-to-omit-properties-38a3ecffe90)_
### Arrays
Remove duplicate objects from an array based on key:
``` js
function removeDupes (array, key) {
return array.filter((obj, index, self) =>
index === self.findIndex((el) => (
el[key] === obj[key]
))
)
}
```**Usage:**
``` js
const someArray = [
{ label: 'greeting', value: 'Hello World' },
{ label: 'greeting', value: 'Hey there' },
{ label: 'greeting', value: 'Hello good person!' },
{ label: 'greeting', value: 'Hello World' },
{ label: 'greeting', value: 'Hello World' },
{ label: 'greeting', value: 'Hello World' },
]removeDupes(someArray, 'value')
```The above would return:
``` js
[
{
label: 'greeting',
value: 'Hello World'
},
{
label: 'greeting',
value: 'Hey there'
},
{
label: 'greeting',
value: 'Hello good person!'
}
]
```_Source: [tutsmake.com](https://www.tutsmake.com/javascript-remove-duplicate-objects-from-array/)_
### Mixed
Get select keys and values from either an object or an array:
``` js
function getSome (original, desired) {
const cleaned = (original && typeof original === 'object' && original.constructor === Array)
? Object.assign({}, ...original)
: originalreturn desired.reduce((obj, key) => ({ ...obj, [key]: cleaned[key] }), {})
}
```