const b = a.{ id, name, age } - Object Cherry Pick

again Haskeller in me came through;)

:sweat_smile::sweat_smile:

1 Like

@DenisTRUFFAUT,

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 :grinning:.

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.

1 Like

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!

1 Like

This seems to be related?

const b = { a.id, a.name, a.age }

Hi all!

Hope to help. I thought about it

option 1

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)

option 2

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' ]
};

option 3


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?

Maybe this idea will be useful with this cases:

case 1: Object Cherry Pick with parameter coercion

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

case2: Object Cherry Pick with Cooperative async function

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())
})

case 3: Object Cherry Pick with Logical Assignment w/ Destructuring

let { id: 42, name: 'a', age: 21, score: { c:1, php: 3, js: 5 }, likes: ['X', 'Y']; } = object;
data.{ id, age }  ||== int; 

case 4: Object Cherry Pick with ternary-like try/catch

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

case 5: object Cherry Pick and Pipeline tap operator |:

const doTransfomration = data => data
  .filter(x => x != null)
  .map(x => x ** 2)
  .filter(x => x > 100)
  .console.log(data.{ id, age })

notes

  1. I would like to demonstrate interesting cases with Object Cherry Pick
  2. I would like to help the community by describing my experience with Object Cherry Pick

references

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>
1 Like

@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 ...

  1. I don't think a separate syntax like that is even needed in JS

    • I thought about that... The idea was to look like LINQ from C#, this may have been an initial mistake. And you are right. Separated syntax might not be a good thing.
  2. 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?

    • That's a great idea. I didn't think about it.
  3. 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.

  • Yeah... what would be very good. But if it helps people, why not use something like this?
  • I didn't invent or create anything... DirectorDoc created it... my idea would be to use the DirectorDoc symbol here... in the Object Cherry pick ... reference ternary-like-try-catch