Quantcast
Channel: how to dynamically add value to object in an array of objects in TypeScript? - Stack Overflow
Viewing all articles
Browse latest Browse all 4

how to dynamically add value to object in an array of objects in TypeScript?

$
0
0

How do I add value to object in an array of objects in Typescript?

Say I have an array of object like this:

[{name:'John',age:20}, {name:'Sam',age:26}, {name:'Will',age:2}]

What I want is dynamically generated property in each object of array as this:

[{name:'John',age:20, value:'John 20'}, {name:'Sam',age:26, value:'Sam 26' }, {name:'Will',age:2, value:'Will 2'}]

This is what I tried: I have a created a model for type checking and created a method in it which will return the value.

export class Names {    name: string;    age: number;    constructor() {    }    getConcat() {        return this.name +''+ this.age;    }}

Although when I get data from server of type Names[], I am not able to fetch the dynamically generated value. I am trying to get values like this:

this.somevariable:Names[]=[];  //declarartionthis.somevariable[0].getConcat() // inside a method after api call

How do I do that? Any help would be much appreciated. Thanks!


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images