2

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

4
  • this is exactly the same as arrays in javascript. though i don't see why you are declaring your objct as an array of boolean, and then overriding it with a n array of object. besides that this. is uneeded Commented Apr 3, 2018 at 5:43
  • each time form submit i will get contactModel object. that object i need update into jsonArrauObject , please help me Commented Apr 3, 2018 at 5:47
  • when i submit form i will get data that i will store like this var var modelData :String={ fname:"vboyini2", lname:"simha2", id:"2", Mobile:"799768999" }:String={ fname:"vboyini2", lname:"simha2", id:"2", Mobile:"799768999" } modelData object asigning into contactModel object, this contactModel object i am pushing into jsonArrayObject,please help me Commented Apr 3, 2018 at 5:48
  • Hi @Vekant B In jsonArrayObject array can have multiple objects so which object you want to unshift? Commented Apr 3, 2018 at 6:05

5 Answers 5

4

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"
});
Sign up to request clarification or add additional context in comments.

4 Comments

i want json object structure like this way: Simply you can push json object into the array like this way. jsonArrayObject = [ contactModel:{ fname:"ABC" } contactModel:{ fname:"zyx" } ]
jsonArrayObject.push( "contactModel" :{ fname:"vboyini2", lname:"simha2", id:"2", Mobile:"799768999" });
@VenkatB Is this the object array you need ?
Did this work for you @VenkatB ? you have to add Quotation marks for "contactModel" when push into array
2

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);

3 Comments

now it is coming different format, it look like this:jsonArrayObject = [ 0:{fname: 'vboyini2', lname: 'simha2', id: '2', Mobile: '799768999'} ] but i need bellow format jsonArrayObject = [ contactModel:{ fname:"ABC" } contactModel:{ fname:"zyx" } ]
no I need bellow format only jsonArrayObject = [ contactModel:{ fname:"ABC" } contactModel:{ fname:"zyx" } ] , how can do in typescript
@venkat-b This is how you should write it to achieve what you want: let jsonArrayObject = [{ contactModel:{fname:"ABC"}},{ contactModel:{fname:"xyz"}}] and when you parse it => "[{"contactModel":{"fname":"ABC"}},{"contactModel":{"fname":"xyz"}}]" The answer is a better way, just rename the modelData
1

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});

1 Comment

contactModel property not exist in IcontactModelData error is coming
0

You don't need to put type in object while unshifting

simply do:

this.jsonArrayObject.unshift({"contactModel":modelData});

where modelData is a variable.

1 Comment

{"contactModel":modelData} in typescript not allowing , it give error
0
let jsonArrayObject=[];
let demo={};
demo={

    id:1,
    "name":"John Doe"
}
jsonArrayObject.push(demo);

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.