Reputation: 41
I am trying to pushing object into existing json data in typescript, i am new to typescript, i created array variable in typescript let jsonArrayObject: boolean[] = [];and this jsonArrayObject contains contactModel object, in this object contain properties like fname,lname, id,mobile. bellow i tried code. please help me.
let jsonArrayObject: boolean[] = [];
jsonArrayObject=[{
contactModel:{
fname:"vboyini",
lname:"simha",
id:"1",
Mobile:"99768999"
}
}];
var modelData :String={
fname:"vboyini2",
lname:"simha2",
id:"2",
Mobile:"799768999"
}
now i want unshift arrayitem that is contactModel object into jsonArrayObject. i tried bellow following code.
this.jsonArrayObject.unshift({"contactModel":any=modelData})
above code is not working. how can i push?please help me any one
Upvotes: 2
Views: 26453
Reputation: 1
let jsonArrayObject=[];
let demo={};
demo={
id:1,
"name":"John Doe"
}
jsonArrayObject.push(demo);
Upvotes: 0
Reputation: 1467
Your scripts are showing compilation error. Cut type of array values are set to Boolean.
let jsonArrayObject: boolean[] = [];
You need to set it to right format.
interface IContactModelData{
fname:string;
lname:string;
id:string;
Mobile:string;
}
interface IContactModel{
contactModel: IContactModelData
}
let jsonArrayObject: IContactModel[] = [];
jsonArrayObject=[{
contactModel:{
fname:"vboyini",
lname:"simha",
id:"1",
Mobile:"99768999"
}
}];
var modelData:IContactModelData = {
fname:"vboyini2",
lname:"simha2",
id:"2",
Mobile:"799768999"
};
jsonArrayObject.push({contactModel:modelData});
Upvotes: 1
Reputation: 148
First of all - you doing it all wrong.
Declare it like this:
let jsonArrayObject = [];
jsonArrayObject = [
{
fname: 'vboyini',
lname: 'simha',
id: '1',
Mobile: '99768999'
}
];
let modelData = {
fname: 'vboyini2',
lname: 'simha2',
id: '2',
Mobile: '799768999'
};
Then you can push modelData in the Array like this, or you can unshift, slice, splice and do whatever you want with the Array
jsonArrayObject.push(modelData);
Upvotes: 2
Reputation: 1119
If you need to push object into array , no need to declare it as a boolean.
let jsonArrayObject = [];
jsonArrayObject.push({
fname:"vboyini2",
lname:"simha2",
id:"2",
Mobile:"799768999"
});
Upvotes: 4
Reputation: 1840
You don't need to put type in object while unshift
ing
simply do:
this.jsonArrayObject.unshift({"contactModel":modelData});
where modelData
is a variable.
Upvotes: 0