Erkan Çalışkan
Erkan Çalışkan

Reputation: 41

How to push object array in array

loggedUser as object in users index (unknown) variable:

// This is the users index of a variable containing multiple users which have a `docs` array.
users: [
    {id: '140255045', password: '72465091Be', email: '[email protected]', tc: '1', dId: 1, name: 'Erkan', surname: 'Çalışkan', docs: []},
    {id: '1', password: '1', email: '[email protected]', tc: '1', dId: 1, name: 'Erkan', surname: 'Çalışkan', docs: []},
    {id: '2', password: '2', email: '[email protected]', tc: '1', dId: 2, name: 'Erkan', surname: 'Çalışkan', docs: []},
    {id: '3', password: '3', email: '[email protected]', tc: '1', dId: 2, name: 'Erkan', surname: 'Çalışkan', docs: []}
]

This is the array I want to push into object:

var docs = [
    {
        type: '',
        piece: '',
        insti: '',
        desc: '',
        selected: '',
        date: ''
    }
];

var a = this.docs.push({
    type: this.docs.type,
    piece: this.docs.piece,
    insti: this.docs.insti,
    desc: this.docs.desc,
    selected: this.docs.selected,
    date: today
});

this.loggedUser.docs.push(a);

console.log(this.loggedUser);

Error : Cannot read property 'push' of undefined

I want to push docs array to loggedUser.docs array.

Upvotes: 4

Views: 950

Answers (3)

Darren Crabb
Darren Crabb

Reputation: 580

If you want to keep users and docs within an object you can do it this way (I've simplified the end code with basic strings to show that it works). I've created another variable called obj to store you're object somewhere.

var obj = {
	users : [
			{id:'140255045',password:'72465091Be',email:'[email protected]',tc:'1',dId:1,name:'Erkan',surname:'Çalışkan',docs:[]},
			{id:'1',password:'1',email:'[email protected]',tc:'1',dId:1,name:'Erkan',surname:'Çalışkan',docs:[]},
			{id:'2',password:'2',email:'[email protected]',tc:'1',dId:2,name:'Erkan',surname:'Çalışkan',docs:[]},
			{id:'3',password:'3',email:'[email protected]',tc:'1',dId:2,name:'Erkan',surname:'Çalışkan',docs:[]}
		  ],
	
	docs:	[{	type : '',
				piece : '',
				insti : '',
				desc : '',
				selected : '',
				date : ''}]	
}
	

obj.docs.push({	type : 'test',
				piece : 'test',
				insti : 'test',
				desc : 'test',
				selected : 'test',
				date : 'test'});
 
		
console.log(obj.docs);

You could also do this with a function within the same object like this

var obj = {
	users : [
			{id:'140255045',password:'72465091Be',email:'[email protected]',tc:'1',dId:1,name:'Erkan',surname:'Çalışkan',docs:[]},
			{id:'1',password:'1',email:'[email protected]',tc:'1',dId:1,name:'Erkan',surname:'Çalışkan',docs:[]},
			{id:'2',password:'2',email:'[email protected]',tc:'1',dId:2,name:'Erkan',surname:'Çalışkan',docs:[]},
			{id:'3',password:'3',email:'[email protected]',tc:'1',dId:2,name:'Erkan',surname:'Çalışkan',docs:[]}
		  ],
	
	docs:	[{	type : '',
				piece : '',
				insti : '',
				desc : '',
				selected : '',
				date : ''}],
	
	a: function () {
		this.docs.push({	type : 'test',
				piece : 'test',
				insti : 'test',
				desc : 'test',
				selected : this.docs[0].selected,
				date : this.docs[0].selected});
		
	}
}
	

obj.a();
 
		
console.log(obj.docs);		

Remember that when referencing the this.docs that it is returning an array so you'll need to make sure you put in index in there for it to refer to a particular object within that array (as I've done in this code on the last couple of values).

Upvotes: 0

James
James

Reputation: 698

If users is an index of a variable, your code should look like this:

// Schema for `this.loggeduser`
{
    id: '140255045',
    password: '72465091Be',
    email: '[email protected]',
    tc: '1',
    dId:1,
    name: 'Erkan',
    surname: 'Çalışkan',
    docs:[]
}

// Declare docs as an `Array` object variable in `this` context.
this.docs = [
    {
        type: '',
        piece: '',
        insti: '',
        desc: '',
        selected: '',
        date: ''
    }
];

// Push an object into the declared array.
// By now `this.docs` should contain 2 objects.
this.docs.push({
    type: this.docs.type,
    piece: this.docs.piece,
    insti: this.docs.insti,
    desc: this.docs.desc,
    selected: this.docs.selected,
    date: today
});

// Push `this.docs` `Array` object.
// If `this.loggedUser` exists and has `docs` property of type `Array` this should work.
this.loggedUser.docs.push(this.docs);

console.log(this.loggedUser)

Upvotes: 0

Pim
Pim

Reputation: 564

Change the colon (:) to an equals sign (=) The colon is used for assigning stuff to an id within an object or array.

users = [
    {id:'140255045',password:'72465091Be',email:'[email protected]',tc:'1',dId:1,name:'Erkan',surname:'Çalışkan',docs:[]},
    {id:'1',password:'1',email:'[email protected]',tc:'1',dId:1,name:'Erkan',surname:'Çalışkan',docs:[]},
    {id:'2',password:'2',email:'[email protected]',tc:'1',dId:2,name:'Erkan',surname:'Çalışkan',docs:[]},
    {id:'3',password:'3',email:'[email protected]',tc:'1',dId:2,name:'Erkan',surname:'Çalışkan',docs:[]}
  ]

Upvotes: 2

Related Questions