Object Extract Method Suggestion
Murat Gözel
murat at gozel.com.tr
Mon May 8 10:36:20 UTC 2017
Hey everyone,
The idea came up while i'm filtering spesific view of web page's text
objects from whole context object. Objects are much more useful than any
other type in many cases. I find myself working with more javascript
objects project to project. Despite this, an object extract method can be
useful for many cases. You can think of it as the opposite of assign
method. It reduces the source object to a smaller object with only keys you
spesified.
Looks like this:
```javascript
Object.extract(SourceObject, ArrayOfKeys)
```
Example usage:
```javascript
const contactPageExpressions = ['Follow Us', 'Phone', 'Email']
const wholeContext = {
'Welcome': {
translation: 'Welcome'
},
'Follow Us': {
translation: 'Follow Us'
},
'Phone': {
translation: 'Phone Number'
},
'Email': {
translation: 'Email Address'
},
'News': {
translation: 'News'
}
}
const activeContext = Object.extract(wholeContext, contactPageExpressions)
// Now we have an object that only has keys we want:
{
'Follow Us': {
translation: 'Follow Us'
},
'Phone': {
translation: 'Phone Number'
},
'Email': {
translation: 'Email Address'
}
}
```
Since it works by matching keys, second parameter of the method can even be
a regular expression:
```javascript
// Get properties that has numbers.
Object.extract(source, /[0-9]/g)
```
Would like to hear your opinions.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170508/1760cc7f/attachment.html>
More information about the es-discuss
mailing list