1

I have this type of array:

0: Client
clientId: 405229
clientName: "Test, Jamie"
1: Client
clientId: 405288
clientName: "Test1, Jamie"
2: Client
clientId: 405239
clientName: "Test3, Jamie"

and I basically want to convert it to be a plain array without the class like

0:
clientId: 405229
clientName: "Test, Jamie"
1:
clientId: 405288
clientName: "Test1, Jamie"
2: 
clientId: 405239
clientName: "Test3, Jamie"

I have tried doing:

Array.map(x=> new Array(x))

but that produce the same result.

any help?

4
  • 3
    What do you mean when you're saying 'but that produce the same result'? Can you share a piece of code that doesn't work/causes typescript compilation errors? FWIW, Array.map(x=> new Array(x)) does nothing useful because Array is a built-in object Commented Nov 23, 2018 at 17:34
  • I basically want to return a flat array of just KeyValue pair and not a concrete class Commented Nov 24, 2018 at 12:07
  • I evened out your downvote and provided a straightforward answer below. IMHO it was an interesting enough question for me to add the frag to my scrap widget in my mono repo. Commented Nov 24, 2018 at 12:55
  • You could just do JSON.parse(JSON.stringify(clientArray)) Commented May 7, 2020 at 17:08

4 Answers 4

3

Here's a nice functional-ish ES6-ish way of going about it:

    // Make the typed array
    const clients : Array<Client> = [];
    for ( let i = 0; i < 10; i++ ) {
      clients.push ( new Client ( i, 'client_' + i.toString () ) );
    }

    // This is the magic line, just spread the object
    const plain = clients.map ( x => ( { ...x } ) );

    // First logs as a typed array, 
    // second as just plain old objects
    console.log ( clients );
    console.log ( plain );

Trace of arrays

Sign up to request clarification or add additional context in comments.

1 Comment

that's the answer! you are a life savior
1

If you want to make it work for any Object I would use the javascript Object.keys which will return you all the object's own property names , read more about it here

Then create an function that will map any class object.

let clientArray : Client[] = [ 
  new Client(24, 'Kobe'), 
  new Client(23, 'Lebron James'),
  new Client(1, 'Zion Williams')
]
let productsArray : Product[] = [ 
  new Product(24, 'Sneakers'), 
  new Product(23, 'Bling),
]

// use this function to map any class to to a simple object.
function mapToSimple(client){ 
    let keys = Object.keys(client)
    let result = {}
    keys.forEach((key) => {
        result[key] = client[key];
    })
    return result;
};

let results = clientArray.map(mapToSimple)
let anotherResults = productsArray.map(mapToSimple)
console.log(results);
console.log(anotherResults);

2 Comments

What exactly is Array here?
the problem is that the objects of the array aren't always known hence the need remove class type
0

Mapping an array of Clients to an array of Client attributes needs the provided function to the map method to pick out the attributes. e.g.

Say there is the following Client class:

class Client {
   clientId: Number;
   clientName: string;

   constructor(clientId: Number, clientName: string) {
      this.clientId = clientId;
      this.clientName = clientName;
   }
}

And there is an initial array of Client instances.

const clientInstances  : Client[] = [ 
  new Client(1, 'Raymond'), 
  new Client(2, 'Damond') 
]

console.log(clientInstances);
// [ Client { clientId: 1, clientName: 'Raymond' },
//   Client { clientId: 2, clientName: 'Damond' } ]

The function provided to the map method is passed each client instance and a new object returned with the client attribute value set for the related key.

interface IClient { 
    clientName: string;
    clientId: Number; 
}

const clientObjects : IClient[] = clientInstances.map(
  client => (
    { clientName: client.clientName, clientId: client.clientId }
  )
)

console.log(clientObjects);
// [ { clientName: 'Raymond', clientId: '1' },
//   { clientName: 'Damond', clientId: '2' } ]

Comments

0
  • For a Pure TS solution where all you care about is the type just Pick it
class Client {
   clientId: number;
   clientName: string;

   constructor(clientId: number, clientName: string) {
      this.clientId = clientId;
      this.clientName = clientName;
   }
}

type ClientArray = Client[];
type JustValues = Pick<Client, 'clientId' | 'clientName'>[]

If you know you want to include all of the values from the Class/interface you can do

type JustValues = Pick<Client, keyof Client>[]

TSPlayground

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.