Object Extract Method Suggestion
Matthew Robb
matthewwrobb at gmail.com
Mon May 8 11:17:20 UTC 2017
Personally I've been itching for object pattern-like pick syntax. I keep
finding myself doing the following pattern:
```
const {a=1,c=2} = obj;
return {a,c};
```
On May 8, 2017 6:37 AM, "Murat Gözel" <murat at gozel.com.tr> wrote:
> 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.
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170508/9252d939/attachment-0001.html>
More information about the es-discuss
mailing list