Change type of an array in typescript
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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?
javascript arrays typescript
add a comment |
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?
javascript arrays typescript
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 becauseArray
is a built-in object
– shkaper
Nov 23 '18 at 17:34
I basically want to return a flat array of just KeyValue pair and not a concrete class
– Jack M
Nov 24 '18 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.
– Tim Consolazio
Nov 24 '18 at 12:55
add a comment |
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?
javascript arrays typescript
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?
javascript arrays typescript
javascript arrays typescript
asked Nov 23 '18 at 17:23
Jack MJack M
1,81641634
1,81641634
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 becauseArray
is a built-in object
– shkaper
Nov 23 '18 at 17:34
I basically want to return a flat array of just KeyValue pair and not a concrete class
– Jack M
Nov 24 '18 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.
– Tim Consolazio
Nov 24 '18 at 12:55
add a comment |
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 becauseArray
is a built-in object
– shkaper
Nov 23 '18 at 17:34
I basically want to return a flat array of just KeyValue pair and not a concrete class
– Jack M
Nov 24 '18 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.
– Tim Consolazio
Nov 24 '18 at 12:55
3
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– shkaper
Nov 23 '18 at 17:34
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– shkaper
Nov 23 '18 at 17:34
I basically want to return a flat array of just KeyValue pair and not a concrete class
– Jack M
Nov 24 '18 at 12:07
I basically want to return a flat array of just KeyValue pair and not a concrete class
– Jack M
Nov 24 '18 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.
– Tim Consolazio
Nov 24 '18 at 12:55
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.
– Tim Consolazio
Nov 24 '18 at 12:55
add a comment |
3 Answers
3
active
oldest
votes
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 );
that's the answer! you are a life savior
– Jack M
Nov 25 '18 at 13:06
add a comment |
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);
What exactly isArray
here?
– shkaper
Nov 23 '18 at 20:54
1
the problem is that the objects of the array aren't always known hence the need remove class type
– Jack M
Nov 24 '18 at 12:06
add a comment |
Mapping an array of Client
s 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' } ]
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450792%2fchange-type-of-an-array-in-typescript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 );
that's the answer! you are a life savior
– Jack M
Nov 25 '18 at 13:06
add a comment |
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 );
that's the answer! you are a life savior
– Jack M
Nov 25 '18 at 13:06
add a comment |
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 );
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 );
edited Nov 24 '18 at 13:01
answered Nov 24 '18 at 12:47
Tim ConsolazioTim Consolazio
2,1091714
2,1091714
that's the answer! you are a life savior
– Jack M
Nov 25 '18 at 13:06
add a comment |
that's the answer! you are a life savior
– Jack M
Nov 25 '18 at 13:06
that's the answer! you are a life savior
– Jack M
Nov 25 '18 at 13:06
that's the answer! you are a life savior
– Jack M
Nov 25 '18 at 13:06
add a comment |
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);
What exactly isArray
here?
– shkaper
Nov 23 '18 at 20:54
1
the problem is that the objects of the array aren't always known hence the need remove class type
– Jack M
Nov 24 '18 at 12:06
add a comment |
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);
What exactly isArray
here?
– shkaper
Nov 23 '18 at 20:54
1
the problem is that the objects of the array aren't always known hence the need remove class type
– Jack M
Nov 24 '18 at 12:06
add a comment |
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);
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);
edited Nov 24 '18 at 12:41
answered Nov 23 '18 at 19:52
Tiisetso TjabaneTiisetso Tjabane
6451716
6451716
What exactly isArray
here?
– shkaper
Nov 23 '18 at 20:54
1
the problem is that the objects of the array aren't always known hence the need remove class type
– Jack M
Nov 24 '18 at 12:06
add a comment |
What exactly isArray
here?
– shkaper
Nov 23 '18 at 20:54
1
the problem is that the objects of the array aren't always known hence the need remove class type
– Jack M
Nov 24 '18 at 12:06
What exactly is
Array
here?– shkaper
Nov 23 '18 at 20:54
What exactly is
Array
here?– shkaper
Nov 23 '18 at 20:54
1
1
the problem is that the objects of the array aren't always known hence the need remove class type
– Jack M
Nov 24 '18 at 12:06
the problem is that the objects of the array aren't always known hence the need remove class type
– Jack M
Nov 24 '18 at 12:06
add a comment |
Mapping an array of Client
s 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' } ]
add a comment |
Mapping an array of Client
s 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' } ]
add a comment |
Mapping an array of Client
s 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' } ]
Mapping an array of Client
s 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' } ]
edited Nov 23 '18 at 18:22
answered Nov 23 '18 at 18:14
Oluwafemi SuleOluwafemi Sule
12.7k1735
12.7k1735
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450792%2fchange-type-of-an-array-in-typescript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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 becauseArray
is a built-in object– shkaper
Nov 23 '18 at 17:34
I basically want to return a flat array of just KeyValue pair and not a concrete class
– Jack M
Nov 24 '18 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.
– Tim Consolazio
Nov 24 '18 at 12:55