-1

I have an entity that contains column users: UserEntity[].

UserEntity has several properties such loginName, userName, email, createdTt, updatedAt ... In response [provide context], I have to fill "users" property only type of UserEntity. But I want to show only loginName, userName, email. Everything else I want to skip. How can I do it if property users can be only UserEntity[] type with all properties? Please help.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Ivan K
  • 1
  • It is possible with Partial typescript: https://stackoverflow.com/questions/61132262/typescript-deep-partial – Siobhan Holubicka Aug 31 '21 at 11:21
  • I'm not finding it clear on if you are trying to send "less" data to an API or just "skip" display of some data returned by an API. Could you please give more context of what data you have, where it comes from or goes to, and how you want to use it? – crashmstr Aug 31 '21 at 11:22
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 03 '21 at 02:00

1 Answers1

1

Suppose you receive an array of UserEntity values called userEntities, where UserEntity is defined something like this:

interface UserEntity {
  loginName: string;
  userName: string;
  email: string;
  createdAt: number;
  updatedAt: number;
  // etc.
}

If you want to create an array containing only some of the properties for each user, the easiest way to do so is to use Array.prototype.map:

const basicUserInfo = userEntities.map(user => ({
  loginName: user.loginName,
  userName: user.userName,
  email: user.email,
}));

map constructs a new array by calling the provided function on each element in the current array. For more info, see MDN.

William Lewis
  • 537
  • 4
  • 9