<div dir="ltr">```from``` implementation using JavaScript<div><br></div><div>```</div><div>const user = {profile:{firstName:"00", lastName:"11"}};<br>const from = (o, ...props) => Object.fromEntries(Object.entries(o).filter(([key]) => props.includes(key)));<br>let obj = {otherData:'other data',...from(user.profile, "firstName")};<br></div><div>```</div><div><br></div><div>which should be possible to code as</div><div><br></div><div><div>```</div><div>const user = {profile:{firstName:"00", lastName:"11"}};</div><div> // preserve right-side/last value syntax to avoid confusion<br>let obj = {otherData:'other data',...{ from user.profile {firstName} }};<br></div><div>```</div></div><div><br></div><div>or, similar to <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Assigning_to_new_variable_names">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Assigning_to_new_variable_names</a></div><div><br></div><div><div>```</div><div>const user = {profile:{firstName:"00", lastName:"11"}};</div><div>let obj = {otherData:'other data'};</div><div>({firstName /* , lastName, ...props */} from user.profile to obj);<br></div><div>```</div></div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, May 26, 2019 at 1:40 PM Григорий Карелин <<a href="mailto:grundiss@gmail.com">grundiss@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Wouldn't it be nice to have syntax like this:<br><span style="background-color:rgb(204,204,204)">const obj = { {firstName, lastName from user.profile}, otherData: 'other data'  };</span><br>as a syntactic sugar for<br><span style="background-color:rgb(204,204,204)">const obj = {firstName: user.profile.firstName, lastName: user.profile.lastName, otherData: 'other data'};</span><br><br>Of cause at the moment we can write it in two steps:<br><span style="background-color:rgb(204,204,204)">const {fistName, lastName} = userProfile;<br>const obj = {firstName, lastName, otherData: 'other data'}<br></span><br>But why use extra variables?<br><br>Motivating example is lodash's .pick() method: <a href="https://lodash.com/docs/#pick" target="_blank">https://lodash.com/docs/#pick</a><br></div>
_______________________________________________<br>
es-discuss mailing list<br>
<a href="mailto:es-discuss@mozilla.org" target="_blank">es-discuss@mozilla.org</a><br>
<a href="https://mail.mozilla.org/listinfo/es-discuss" rel="noreferrer" target="_blank">https://mail.mozilla.org/listinfo/es-discuss</a><br>
</blockquote></div>