Javascript 如何在数组中推送对象数组
Javascript 如何在数组中推送对象数组,javascript,arrays,vue.js,Javascript,Arrays,Vue.js,loggedUserasobjectinusersindex(未知)变量: // This is the users index of a variable containing multiple users which have a `docs` array. users: [ {id: '140255045', password: '72465091Be', email: 'a@gmail.com', tc: '1', dId: 1, name: 'Erkan', surname:
loggedUser
asobject
inusers
index(未知)变量:
// This is the users index of a variable containing multiple users which have a `docs` array.
users: [
{id: '140255045', password: '72465091Be', email: 'a@gmail.com', tc: '1', dId: 1, name: 'Erkan', surname: 'Çalışkan', docs: []},
{id: '1', password: '1', email: 'a@gmail.com', tc: '1', dId: 1, name: 'Erkan', surname: 'Çalışkan', docs: []},
{id: '2', password: '2', email: 'a@gmail.com', tc: '1', dId: 2, name: 'Erkan', surname: 'Çalışkan', docs: []},
{id: '3', password: '3', email: 'a@gmail.com', tc: '1', dId: 2, name: 'Erkan', surname: 'Çalışkan', docs: []}
]
这是我要推入对象的数组:
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);
错误:无法读取未定义的属性“push”
我想将docs
数组推送到loggedUser.docs
数组。将冒号(:
)更改为等号(=
)
冒号用于将内容分配给对象或数组中的id
users = [
{id:'140255045',password:'72465091Be',email:'a@gmail.com',tc:'1',dId:1,name:'Erkan',surname:'Çalışkan',docs:[]},
{id:'1',password:'1',email:'a@gmail.com',tc:'1',dId:1,name:'Erkan',surname:'Çalışkan',docs:[]},
{id:'2',password:'2',email:'a@gmail.com',tc:'1',dId:2,name:'Erkan',surname:'Çalışkan',docs:[]},
{id:'3',password:'3',email:'a@gmail.com',tc:'1',dId:2,name:'Erkan',surname:'Çalışkan',docs:[]}
]
如果
users
是变量的索引,则代码应如下所示:
// Schema for `this.loggeduser`
{
id: '140255045',
password: '72465091Be',
email: 'a@gmail.com',
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)
如果您想将用户和文档保存在一个对象中,您可以这样做(我用基本字符串简化了最终代码,以表明它是有效的)。我创建了另一个名为obj的变量来存储对象
var obj={
用户:[
{id:'140255045',密码:'72465091Be',电子邮件:'a@gmail.com,tc:'1',dId:1,姓名:'Erkan',姓氏:'Chalışkan',文件:[]},
{id:'1',密码:'1',电子邮件:'a@gmail.com,tc:'1',dId:1,姓名:'Erkan',姓氏:'Chalışkan',文件:[]},
{id:'2',密码:'2',电子邮件:'a@gmail.com,tc:'1',dId:2,姓名:'Erkan',姓氏:'Chalışkan',文件:[]},
{id:'3',密码:'3',电子邮件:'a@gmail.com,tc:'1',dId:2,姓名:'Erkan',姓氏:'Chalışkan',文件:[]
],
文档:[{类型:“”,
作品:'',
灌输:'',
描述:'',
已选择:“”,
日期:'}]
}
obj.docs.push({type:'test',
作品:"测试",,
灌输:‘测试’,
描述:“测试”,
已选择:“测试”,
日期:'测试'});
控制台日志(对象文档)
您的loggedUser
看起来像数据对象中的wha?这在语法上似乎不正确,但很难判断它是否只是从elwhere中剥离出来的代码片段。看起来你已经用用户和文档的属性定义了一个对象,但是你突然有了函数代码而没有终止对象定义?我可能希望在文档定义后加一个逗号,然后在最后一位加一个函数定义?否则“this”指的是什么?顺便说一句-错误基本上意味着它不知道this.docs是什么-它是未定义的。在对未定义的位进行排序之前,推送部分在这里是不相关的。我认为users
是数据对象属性,而不是局部变量