again Haskeller in me came through;)
![]()
![]()
again Haskeller in me came through;)
![]()
![]()
Will nested picking like this work?
const newProps = prop.{ nested.{ a, b }, others };
If so, we could also do nested destructuring!
const { nested.{ a, b }, others } = prop;
Thanks
.
I think it is a different proposal, that I would call Object Cherry Assignment.
It does not create a new Object, but instead assigns properties chirurgically.
In that mindset, I think these 2 separated examples could work.
The second example is a little bit extreme because properties names have to match at 100% and renaming would use the dereferencing renaming token : which can be confusing with the Force value token in the Object Cherry Pick proposal (this proposal). But if we consider that these proposals are 2 different proposals, maybe. After all, dereferencing and object creation are just nowadays inconsistent in their assignment tokens (: for object creation and = for dereferencing) and nobody complains (except me).
We need more extreme examples to find if it covers all the weird cases.
Yes, this idea has been floating around for years. There are several lengthy threads on the old esdiscuss mailing list. One proposal is at GitHub - rtm/js-pick-notation: Proposal for Picked Properties in JavaScript.
I have never understood why this idea did not get more traction. By all the yardsticks by which one assesses language proposals, it scores at least as well or better than other features already adopted and implemented and in widespread use. Could it be that potential champions are busy with their own proposals?
I can say that from the standpoint of selling the idea, I have reached the conclusion that it is better (for now) not to overcomplicate it with additional features such as renaming, defaults, application to arrays, etc., as obvious and useful as those may be.
Congratulations on the cool name "cherry pick" though!
This seems to be related?
const b = { a.id, a.name, a.age }
Hi all!
Hope to help. I thought about it
class Data
id: '1'
name: 'a'
age: 21
class Score
c: 1
php: 3
js: 5
class Likes
array: ['X', 'Y']
pick: (id, name, age) -> Class: Data, Score, Likes
return id, name, age
This idea would be close to CoffeeScript: Constants in CoffeeScript (Example)
class Data {
id, name, age
}
class Score {
c, php, js
}
class Likes {
array
}
// Object Cherry Pick
const b= {
Data.id: 42,
Data.name: 'a',
Data.age: 21,
Score.c:1,
Score.php: 3,
Score.js: 5,
Likes: ['X', 'Y' ]
};
class Data {
constructor(id, name, age) {
this.id = id;
this.name = name;
this.age = age;
}
}
class Score {
constructor(c, php, js) {
this.c = c;
this.php = php;
this.js = js;
}
}
class Likes {
constructor(array) {
return this.array = array;
}
}
class ObjectCherryPick {
Data.id: 42,
Data.name: 'a',
Data.age: 21,
Score.c:1,
Score.php: 3,
Score.js: 5,
Likes: ['X', 'Y' ]
}
const a = { { Data }, { Score }, { Likes } }; // { id: 42, name: 'a', age: 21, score: { c:1, php: 3, js: 5 }, likes: ['X', 'Y'] };
// or opt1: ObjectCherryPick
const op1 = { Data, Score, Likes }; // const a = { id: 42, name: 'a', age: 21, score: { c:1, php: 3, js: 5 }, likes: ['X', 'Y'] };
// example op1
op1 .{ id, name, age };
// or example op2: ObjectCherryPick
const op2= op1.{ id, name, age };
// or example op3
const op3= ObjCherryPick.{ Data.id, Data.name, Data.age };
// or example op4: ObjectCherryPick
const opt4 = a.{ id, name, age };
// or example op5: ObjectCherryPick
const op5= { Data.id, Data.name, Data.age };
Inspiration: TypeScript, TypeScript-classes,
@DenisTRUFFAUT Hi! What do you think these ideas?
const adults
from data => { id: 42, name: 'a', age: 21, score: { c:1, php: 3, js: 5 }, likes: ['X', 'Y'] };
from users => users.filter(u => u.age > 18)
from conf => conf.users
from filter => data.{ id, name, age };
from JSON.parse
= rawData
useEffect(function*() {
const data = { id: 42, name: 'a', age: 21, score: { c:1, php: 3, js: 5 }, likes: ['X', 'Y'] };
const info = yield data.{ id, name, age }
setState(info.json())
})
let { id: 42, name: 'a', age: 21, score: { c:1, php: 3, js: 5 }, likes: ['X', 'Y']; } = object;
data.{ id, age } ||== int;
const data = { id: 42, name: 'a', age: 21, score: { c:1, php: 3, js: 5 }, likes: ['X', 'Y'] };
let success = try? console.log(data.{ id, age }) : false
|:const doTransfomration = data => data
.filter(x => x != null)
.map(x => x ** 2)
.filter(x => x > 100)
.console.log(data.{ id, age })
case 1 of your's looks like LINQ from C#, I've been using it for a while now (a necessary evil more or lessπ ). I don't think a separate syntax like that is even needed in JS. However a general mechanism for querying collections or in this case objects is what we want. Why not aim for a more general approach of querying objects like C# does with LINQ instead of restricting to only pick and pluck operations?
case 4 is particularly interesting; people here have been looking for a compact expression level error handling mechanism. This is however not related to this particular idea. But I really like the syntax you proposed.
try? <expression> : <expression>
@jithujoshyjy Hi! How are you? Thank you very much for the feedback ... And ... Yeah ... I got inspiration with C# in case 1 (a necessary evil more or lessπ ) yeah ...
So ...
I don't think a separate syntax like that is even needed in JS
However a general mechanism for querying collections or in this case objects is what we want. Why not aim for a more general approach of querying objects like C# does with LINQ instead of restricting to only pick and pluck operations?
case 4 is particularly interesting; people here have been looking for a compact expression level error handling mechanism. This is however not related to this particular idea. But I really like the syntax you proposed.