如何将对象数组推入数组
Posted
技术标签:
【中文标题】如何将对象数组推入数组【英文标题】:How to push object array in array 【发布时间】:2019-03-24 12:37:40 【问题描述】:loggedUser
as object
in users
索引(未知)变量:
// 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);
错误:无法读取未定义的属性“推送”
我想将docs
数组推送到loggedUser.docs
数组。
【问题讨论】:
您的loggedUser
在您的数据对象中看起来像什么?
这似乎在语法上不正确,但很难判断它是否只是从其他地方剥离的代码片段。看起来您已经完成了定义具有用户和文档属性的对象的一半,但是您突然有了功能代码而没有终止对象定义?我可能希望在文档定义之后有一个逗号,然后可能是最后一位的函数定义?否则“this”指的是什么?
BTW - 错误基本上意味着它不知道 this.docs 是什么 - 它是未定义的。推送部分在这里是无关紧要的,直到您将未定义的位排除在外。
【参考方案1】:
将冒号 (:
) 更改为等号 (=
)
冒号用于将内容分配给对象或数组内的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
是数据对象属性而不是局部变量【参考方案2】:
如果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)
【讨论】:
【参考方案3】:如果您想将用户和文档保留在一个对象中,您可以这样做(我已经用基本字符串简化了结束代码以表明它可以工作)。我创建了另一个名为 obj 的变量来将您的对象存储在某处。
var obj =
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:[]
],
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);
您也可以使用同一对象中的函数来执行此操作
var obj =
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:[]
],
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);
请记住,当引用 this.docs 时,它会返回一个数组,因此您需要确保在其中放入索引以引用该数组中的特定对象(正如我在本文中所做的那样最后几个值的代码)。
【讨论】:
以上是关于如何将对象数组推入数组的主要内容,如果未能解决你的问题,请参考以下文章